如何从 MongoDB 文档中完全删除字段? [英] How to remove a field completely from a MongoDB document?
问题描述
{
name: 'book',
tags: {
words: ['abc','123'],
lat: 33,
long: 22
}
}
假设这是一个文档.如何从该集合中的所有文档中完全删除words
"?我希望所有文档都没有words
":
Suppose this is a document. How do I remove "words
" completely from all the documents in this collection? I want all documents to be without "words
":
{
name: 'book',
tags: {
lat: 33,
long: 22
}
}
推荐答案
试试这个:如果你的收藏是示例"
Try this: If your collection was 'example'
db.example.update({}, {$unset: {words:1}}, false, true);
参考:
http://www.mongodb.org/display/DOCS/Updating#Updating-%24unset
更新:
以上链接不再涵盖$unset".如果要从集合中的所有文档中删除此字段,请务必添加 {multi: true}
;否则,它只会从它找到匹配的第一个文档中删除它.请参阅此以获取更新的文档:
The above link no longer covers '$unset'ing. 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/更新/未设置/
示例:
db.example.update({}, {$unset: {words:1}} , {multi: true});
这篇关于如何从 MongoDB 文档中完全删除字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!