1
votes

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'})
2

2 Answers

1
votes

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'})
});
1
votes

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