How to remove document from all collections in Mon

2019-04-16 13:42发布

问题:

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.