{ 
    name: 'book',
    tags: {
        words: ['abc','123'],
        lat: 33,
        long: 22
    }
}

假设这是一个文档。我如何从这个集合中的所有文档中完全删除“单词”?我希望所有的文件都没有“文字”:

 { 
     name: 'book',
     tags: {
         lat: 33,
         long: 22
     }
}

当前回答

您也可以使用3.4中的project在聚合中实现这一点

{$项目:{"标签。0}}

其他回答

删除或删除MongoDB中的字段

单次记录 db.getCollection(“用户数据”)。更新({},{$unset: {pi: 1}}) 多重记录 db.getCollection(“用户数据”)。更新({},{$unset: {pi: 1}}, {multi: true})

对于monomapper来说,

文档:关闭 需要移除的字段:shutoff_type

Shutoff.collection。更新 ( {}, { '$ 设置' = >{“shutoff_type”:1}},:多= > true)

检查“words”是否存在,然后从文档中删除

    db.users.update({"tags.words" :{$exists: true}},
                                           {$unset:{"tags.words":1}},false,true);

True表示匹配时更新多个文档。

要删除一个字段,您可以使用这个命令(这将应用于集合中的所有文档):

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

在一个命令中删除多个字段:

db.getCollection('example').update({}, {$unset: {Words:1 ,Sentences:1}}, {multi: true});

从Mongo 4.2开始,也可以使用稍微不同的语法:

// { name: "book", tags: { words: ["abc", "123"], lat: 33, long: 22 } }
db.collection.updateMany({}, [{ $unset: ["tags.words"] }])
// { name: "book", tags: { lat: 33, long: 22 } }

由于更新方法可以接受聚合管道(注意方括号表示使用聚合管道),这意味着这里使用的$unset操作符是聚合操作符(而不是“查询”操作符),其语法接受一个字段数组。