问题
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