Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove multiple documents from mongo in a single query

I have a list of mongo '_id' which I want to delete. Currently I am doing this

# inactive_users -->  list of inactive users  for item in inactive_users:     db.users.remove({'_id' : item}) 

but my problem is the list is too huge... (it might go 100,000 +). So querying for every item in list will only increase the load on server. Is their a way to pass the entire list in mongo query so that I dont have to fire query again and again.

Thank you

like image 308
Anurag Sharma Avatar asked Sep 02 '13 06:09

Anurag Sharma


People also ask

How do I remove all files from a collection in MongoDB?

To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.

How do I delete multiple ids in MongoDB?

You can accomplish this by using the command deleteMany.

How do I skip the first 5 documents in MongoDB?

To skip records in MongoDB, use skip(). With that, to display only a specific number of records, use limit().


Video Answer


1 Answers

db.users.deleteMany({'_id':{'$in':inactive_users}}) 
like image 136
Roman Pekar Avatar answered Sep 29 '22 18:09

Roman Pekar