想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .
关闭 8 年前。
我从文档定义中删除了一些字段。我想在所有集合文档中删除此字段。我该怎么做?
最佳答案
试试:
db.collection.update(
{ '<field>': { '$exists': true } }, // Query
{ '$unset': { '<field>': true } }, // Update
false, // Upsert
true // Multi-update
)
其中 field
是您已弃用的字段,而 collection
是它被删除的集合。
一般更新命令的格式为 db.collection.update(criteria, objNew, upsert, multi)
。 false
和 true
尾随参数禁用 upsert 模式并启用多重更新,以便查询更新集合中的所有文档(不仅仅是第一个匹配项)。
MongoDB 2.2+ 更新
您现在可以为 upsert 和 multi 提供 JSON 对象而不是位置参数。
db.collection.update(
{ '<field>': { '$exists': true } }, // Query
{ '$unset': { '<field>': true } }, // Update
{ 'multi': true } // Options
)
https://stackoverflow.com/questions/8641340/