如何从MongoDB文档中完全删除字段?

时间:2011-07-27 22:17:51

标签: mongodb mongodb-query database

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

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

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

15 个答案:

答案 0 :(得分:437)

试试这个:如果你的收藏是'示例'

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

参考:

http://www.mongodb.org/display/DOCS/Updating#Updating-%24unset

更新

以上链接不再涵盖'$ unset'。如果要从集合中的所有文档中删除此字段,请务必添加{multi: true};否则,它只会从找到匹配的第一个文档中删除它。有关更新的文档,请参阅此页:

https://docs.mongodb.com/manual/reference/operator/update/unset/

示例:

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

答案 1 :(得分:144)

一开始,我不明白为什么这个问题有一个赏金(我认为这个问题有一个很好的答案,没有什么可补充的),但后来我注意到被接受和赞成15次的答案是实际上错了!

是的,你必须使用$unset operator,但是这个未设置将删除对于集合的文档不存在的单词key。所以基本上它什么都不做。

因此,您需要告诉Mongo查看文档标签,然后使用dot notation查找单词。所以正确的查询是。

db.example.update(
  {},
  { $unset: {'tags.words':1}},
  false, true
)

仅仅为了完成,我将引用another way of doing it,这更糟糕,但是这样你可以用任何自定义代码更改字段(甚至基于本文档中的另一个字段)。

答案 2 :(得分:15)

删除或删除MongoDB中的字段

  • 单曲

    db.getCollection('userData').update({}, {$unset: {pi: 1}})
    
  • 对于多记录

    db.getCollection('userData').update({}, {$unset: {pi: 1}}, {multi: true})
    

答案 3 :(得分:11)

db.example.updateMany({},{"$unset":{"tags.words":1}})

我们也可以使用它来更新多个文档。

答案 4 :(得分:3)

PyMongo(Python mongo)的解决方案:

db.example.update({}, {'$unset': {'tags.words':1}}, multi=True);

答案 5 :(得分:2)

由于我一直在寻找一种使用MongoEngine删除字段的方法时一直在查找此页面,因此我认为在此处发布MongoEngine的方法也可能会有所帮助:

$ awk -v RS='([^|]*[|]){7}[^\n]*\n' '{$0=RT; $1=$1; gsub(/ *[|] */,"|")}1' file
AA|5904060|9001084471200270|9000263372600200|Result Comment: No (1, 3) Beta-D-Glucan detected. This assay does not detect certain fungi, including Cryptococcus species, which produce very low levels of (1, 3) Beta-D-Glucan (BDG) and the Mucorales (e.g., Lichthemia, Mucor and Rhizopus), which are not known to produce BDG. Additionally, the yeast phase of Blastomyces dermatitidis produces little BDG and may not be detected by this assay.|North Building|0|0

答案 6 :(得分:1)

我试图做与此类似的事情,但是从嵌入式文档中删除了该列。我花了一段时间才找到解决方案,这是我遇到的第一篇文章,所以我认为我会把这篇文章发给其他尝试这样做的人。

因此,可以说您的数据看起来像这样:

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

要从嵌入式文档中删除列words,请执行以下操作:

db.example.update(
  {'tags': {'$exists': true}},
  { $unset: {'tags.$[].words': 1}},
  {multi: true}
)

或使用updateMany

db.example.updateMany(
  {'tags': {'$exists': true}},
  { $unset: {'tags.$[].words': 1}}
)

$unset仅在值存在时对其进行编辑,但不会进行安全导航(它不会首先检查tags是否存在),因此嵌入式文档中需要该值。

这使用了在版本3.6中引入的all positional operator$[]

答案 7 :(得分:1)

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

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

update方法也可以接受聚合管道(请注意方括号表示使用聚合管道)。

这意味着正在使用的$unset运算符是聚合运算符(与"query" one相对),其语法采用字段数组。

答案 8 :(得分:1)

mongoDB shell 中,此代码可能会有所帮助:

db.collection.update({}, {$unset: {fieldname: ""}} )

答案 9 :(得分:0)

默认情况下,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})

答案 10 :(得分:0)

对于mongomapper,

  • 文档:关闭
  • 要删除的字段:shutoff_type

Shutoff.collection.update( {}, { '$unset' => { 'shutoff_type': 1 } }, :multi => true )

答案 11 :(得分:-1)

{     名称:“ book”,     标签:{         字词:['abc','123'],         经度:33,         长:22     } }

回答:

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

答案 12 :(得分:-3)

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

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

true表示如果匹配则更新多个文档。

答案 13 :(得分:-4)

您也可以使用3.4

中的项目进行聚合

{$ project:{&#34; tags.words&#34;:0}}

答案 14 :(得分:-7)

引用包并删除各种&#34;键&#34;, 试试这个

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