Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete multiple ids in mongodb?

Tags:

{ "_id" : ObjectId("51ee3966e4b056fe8f074f48"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"} { "_id" : ObjectId("51ee507ae4b056fe8f074f4a"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"} { "_id" : ObjectId("51ee51fee4b056fe8f074f4b"), "userid" : "66", "clientid" : "88", "deviceid" : "22", "timestamp" : "1374214822000"} 

How to delete multiple ids in mongodb?

like image 512
user1983299 Avatar asked Jul 24 '13 05:07

user1983299


People also ask

How do I delete a list in MongoDB?

To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

What does deleteMany return in mongoose?

The deleteMany() method returns an object that contains three fields: n – number of matched documents. ok – 1 if the operation was successful. deletedCount – number of deleted documents.

Which command will remove all documents in a collection with field age set to 10?

remove() The remove() method removes documents from the database. It can remove one or all documents from the collection that matches the given query expression.


2 Answers

By running remove once for each ID, or perhaps using in:

db.collection.remove( { _id : { $in: [     ObjectId("51ee3966e4b056fe8f074f48"),      ObjectId("51ee3966e4b056fe8f074f4a"),      ObjectId("51ee3966e4b056fe8f074f4b")  ] } } ); 
like image 98
Derick Avatar answered Oct 13 '22 00:10

Derick


You can accomplish this by using the command deleteMany.

const objects = [     ObjectId("51ee3966e4b056fe8f074f48"),      ObjectId("51ee3966e4b056fe8f074f4a"),      ObjectId("51ee3966e4b056fe8f074f4b")  ];  db.collection.deleteMany({_id: { $in: objects}}); 
like image 21
Kusal Hettiarachchi Avatar answered Oct 13 '22 00:10

Kusal Hettiarachchi