I want to implement a delete method which deletes all the documents in a collection. I am using mongo db with Spring Data.This could be done using db.myCollection.remove({}) in Mongo shell. But I want to write a method in my data access layer to do this.I am not using MongodbTemplate in my Dao class. I want to know how can I do this with Query.
Query query = new Query();
Could anybody please tell me how can I do it.
Here is the command to delete all the records of a collection in the MongoDB shell or MongoDB Compass shell (_MongoSH). show dbs -> to list all the database use test-database -> to select the database db. getCollection('orders'). deleteMany({}) -> to select the collection and deletes all the data.
MongoDB's db. collection. drop() is used to drop a collection from the database.
You actually can't bulk delete in MongoDB Atlas. See MongoDB Atlas info on the filter section AKA Data Explorer. However you can run standard queries, like find, remove once you connect to the database using your Atlas credentials.
MongoDB's remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag. deletion criteria − (Optional) deletion criteria according to documents will be removed.
You can use MongoTemplate
directly
mongoTemplate.remove(new Query(), collectionName);
You can drop the collection and create it again:
mongoTemplate.dropCollection("collectionName");
mongoTemplate.createCollection("collectionName");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With