Daniel Jeney
Daniel Jeney

Reputation: 506

Mongo/Spring boot delete all documents in a list from a collection

Is there a way to delete all documents of a list(List) from a collection in bulk? I am thinking of something like: mongooperations.deleteAll(list);

It doesnt have to be a List tho, just any collection that i can collect the documents in and delete them in bulk, instead of deleting always single documents.

Upvotes: 2

Views: 6706

Answers (1)

prasad_
prasad_

Reputation: 14287

Here is the query to delete a list of document _ids. Assuming the _id's are numbers, this works:

List<Integer> ids = Arrays.asList(1, 2, 3, 4);
MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "testDB");
Query q = new Query(where("_id").in(ids));
List<Test> deletedDocs = mongoOps.findAllAndRemove(q, Test.class, "testColl");
// -or-
//List<Document> deletedDocs = mongoOps.findAllAndRemove(q, "testColl");
System.out.println(deletedDocs);

Upvotes: 1

Related Questions