27
{ "_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?

2 Answers 2

57

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

db.collection.remove( { _id : { $in: [
    ObjectId("51ee3966e4b056fe8f074f48"), 
    ObjectId("51ee3966e4b056fe8f074f4a"), 
    ObjectId("51ee3966e4b056fe8f074f4b") 
] } } );
Sign up to request clarification or add additional context in comments.

5 Comments

mr.derick is there any data types in mongodb?
Yes, there are, but that has nothing to do with this question.
while inserting data into mongodb datatypes are not mandatory i think so..what is your opinion...
Is the same available with NotIn as well?
remove is deprecated! Should be used deleteMany
34

You can accomplish this by using the command deleteMany.

const objects = [
    ObjectId("51ee3966e4b056fe8f074f48"), 
    ObjectId("51ee3966e4b056fe8f074f4a"), 
    ObjectId("51ee3966e4b056fe8f074f4b") 
];

db.collection.deleteMany({_id: { $in: objects}});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.