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

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

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

当前回答

值得一提的是,如果你使用Typegoose/Mongoose,你的字段需要在你的模型中声明,否则$unset将默默失败。

在重构数据库后,这让我没有意识到。多余的字段不再在我的模型中,我正在运行$unset,但什么也没有发生。

因此,如果你正在进行一些重构,请确保在从模型中删除冗余字段之前将其$unset。

其他回答

db.collection.updateMany({}, {$unset: {"fieldName": ""}})

updateMany要求每个文档都有一个匹配条件,因为我们传递的是{},它总是true。第二个参数使用$unset操作符删除每个文档中的必需字段。

要引用一个包并删除各种“键”, 试试这个

db['name1.name2.name3.Properties'].remove([
{
     "key" : "name_key1"
},
{
     "key" : "name_key2"
},
{
     "key" : "name_key3"
}
)]

默认情况下,update()方法更新单个文档。设置“多参数”以更新符合查询条件的所有文档。

在3.6版更改。 语法:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ]
   }
)

例子:

db.getCollection('products').update({},{$unset: {translate:1, qordoba_translation_version:1}}, {multi: true})

在你的例子中:

db.getCollection('products').update({},{$unset: {'tags.words' :1}},  {multi: true})

对于monomapper来说,

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

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

{ 名称:“书”, 标签:{ 词:[' abc ', ' 123 '), 纬度:33岁 长:22 } }

Ans:

db.tablename.remove ({tags.words: [' abc ', ' 123 ']})