如何从MongoDB文档中完全删除字段? [英] How to remove a field completely from a MongoDB document?

查看:362
本文介绍了如何从MongoDB文档中完全删除字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{ 
    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-%24未设置

更新:

以上链接不再涵盖"$ 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 /update/unset/

示例:

db.example.update({}, {$unset: {words:1}} , {multi: true});

这篇关于如何从MongoDB文档中完全删除字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆