How to remove document from all collections in Mongo database

十年热恋 提交于 2019-12-21 23:45:04

问题


I have Mongo database with 16 collections. All collection has the common field domain_id. How can I remove documents with specified domain_id from all collections.

I know only how to remove document from single collection.

db.getCollection('collectionName1').remove({domain_id : '123'})

回答1:


Use the method db.getCollectionNames() to get a list of all the collections in your database, iterate the list using the JavaScript's forEach() method to remove the document from each collection:

db.getCollectionNames().forEach(function (col) {
    db.getCollection(col).remove({domain_id : '123'})
});



回答2:


Unfortunately, Mondo doesn't allow for linking collections. So, you do have to do it for each separate collection.



来源:https://stackoverflow.com/questions/36845339/how-to-remove-document-from-all-collections-in-mongo-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!