2011-12-27 53 views

回答

112

尝试:

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)。尾部参数falsetrue禁用upsert模式并启用多重更新,以便查询更新集合中的所有文档(而不仅仅是第一个匹配项)。

更新MongoDB的2.2+

您现在可以提供一个JSON对象,而不是用于UPSERT和多位置参数。

db.collection.update(
    { '<field>': { '$exists': true } }, // Query 
    { '$unset': { '<field>': true } }, // Update 
    { 'multi': true }     // Options 
) 
+3

为$ unset语句中的字段值(即1)指定的值不会影响操作。 [$ unset operator](http://docs.mongodb.org/manual/reference/operator/unset/#_S_unset) – Xiao 2013-01-14 10:24:02

29

哦了$unset答案有人给使用update()here是相当真棒太刚做这样的事情

db.people.find().forEach(function(x) { 
    delete x.badField; 
    db.people.save(x); 
}) 

+0

THX,Jamund。你的方法也适用。 – 2011-12-29 08:33:49

+0

无法接受2个答案。所以刚刚提出了它。 – 2011-12-29 08:34:17

+0

谢谢,这非常友善! – 2011-12-29 16:32:28

相关问题