如何从Mongo中完全删除字段? [英] How do I remove a field completely from Mongo?
问题描述
{
name: 'book',
tags: {
words: ['abc','123'],
lat: 33,
long: 22
}
}
假设这是一个文档。如何从该集合中的所有文档中完全删除单词?我想要所有的文件没有字:
Suppose this is a document. How do I remove "words" completely from all the documents in this collection? I want all the documents to be without "words":
{
name: 'book',
tags: {
lat: 33,
long: 22
}
}
$ b b
推荐答案
尝试:如果您的集合是example
Try this: If your collection was 'example'
db.example.update({}, {$unset: {words:1}}, false, true);
参考:
http://www.mongodb.org/display/DOCS/Updating#Updating-%24unset
UPDATE :
上述链接不再涵盖'$取消设置。下面的Nic Cottrell的评论是现在的方式。如果要从集合中的所有文档中删除此字段,请务必添加 {multi:true}
否则,它将只从它发现匹配的第一个文档中删除它。查看更新的文档:
The above link no longer covers '$unset'ing. Nic Cottrell's comment below is the way to go now. Be sure to add {multi: true}
if you want to remove this field from all of the documents in the collection; otherwise, it will only remove it from the first document it finds that matches. See this for updated documentation:
https://docs.mongodb.com/manual/reference/operator/update/unset/
示例:
db.example.update({}, {$unset: {words:1}} , {multi: true});
这篇关于如何从Mongo中完全删除字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!