MongoDB - 是否可以删除所有引用了特定字段的文档?

MongoDB - Is it possible to remove all documents with a certain field referenced?

我有 Documents,它有另一个 Document 的参考键。 是否可以一次性删除所有引用该字段的文档?

现在,我会在每个集合中使用 db.collection.remove(<query for the reference>) 和 运行,但这似乎根本没有效率。

我想做的是使用一个删除查询,该查询将 运行 遍历所有文档。我在 RoboMongo 中使用命令提示符。

你第一个问题的答案是是的你可以做到。

怎么做: 如果你想执行那样的操作,你可以写一个小的 mongoDB 脚本。 learn more about mongo shell scripts

您的 post 的解决方案可能是这样的脚本:

cursor = db.collection.find();
while ( cursor.hasNext() ) {
   // Compare the two keys and remove the appropriate document
}
如果您必须自动执行操作,

MongoDB Shell Scripting 是一个很好的解决方案。