如何在Mongo中删除不推荐使用的字段? [英] How to remove deprecated fields in Mongo?
问题描述
我从文档定义中删除了一些字段.我想在所有托收文件中删除此字段.我该怎么办?
I have removed some fields from document definition. I want to remove this field across all documents of collection. How can I do it?
推荐答案
尝试:
db.collection.update(
{ '<field>': { '$exists': true } }, // Query
{ '$unset': { '<field>': true } }, // Update
false, // Upsert
true // Multi-update
)
其中field
是您不赞成使用的字段,而collection
是已将其从其中删除的集合.
where field
is your deprecated field and collection
is the collection it was removed from.
常规更新命令的格式为db.collection.update( criteria, objNew, upsert, multi )
. false
和true
尾随参数禁用upsert模式并启用多重更新,以便查询更新集合中的所有文档(而不仅仅是第一个匹配项).
The general update command is of the form db.collection.update( criteria, objNew, upsert, multi )
. The false
and true
trailing arguments disable upsert mode and enable multi update so that the query updates all of the documents in the collection (not just the first match).
针对MongoDB 2.2+的更新
您现在可以提供JSON对象,而不是用于upsert和multi的位置参数.
You can now provide a JSON object instead of positional arguments for upsert and multi.
db.collection.update(
{ '<field>': { '$exists': true } }, // Query
{ '$unset': { '<field>': true } }, // Update
{ 'multi': true } // Options
)
这篇关于如何在Mongo中删除不推荐使用的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!