如何从Mongo数据库中的所有集合中删除文档 [英] How to remove document from all collections in Mongo database
本文介绍了如何从Mongo数据库中的所有集合中删除文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 Mongo数据库,其中包含16个收藏集.所有集合都具有公共字段domain_id
.
如何从所有集合中删除具有指定domain_id
的文档.
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'})
推荐答案
使用方法 forEach()
从每个集合中删除文档的方法:
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'})
});
这篇关于如何从Mongo数据库中的所有集合中删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文