从mongodb数组中的所有元素中删除一个字段 [英] Remove a field from all elements in array in mongodb

查看:25
本文介绍了从mongodb数组中的所有元素中删除一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MongoDB(2.4.5) 中有以下文档

I have below document in MongoDB(2.4.5)

{
    "_id" : 235399,
    "casts" : {
        "crew" : [ 
            {
                "_id" : 1186343,
                "withBase" : true,
                "department" : "Directing",
                "job" : "Director",
                "name" : "Connie Rasinski"
            },
            {
                "_id" : 86342,
                "withBase" : true
            }
        ]
    },
    "likes" : 0,
    "rating" : 0,
    "rating_count" : 0,
    "release_date" : "1955-11-11"
}

我想从 casts.crew 内的数组元素中删除 withBase 字段 ..

I want to remove withBase filed from array elements inside casts.crew ..

我试过了

db.coll.update({_id:235399},{$unset: {  "casts.crew.withBase" : 1 } },false,true)

什么都没有改变.

并尝试了这个..

db.coll.update({_id:235399},{$unset: {  "casts.crew" : { $elemMatch: { "withBase": 1 } } } },false,true)

它从文档中删除了整个乘员组.

it removed entire crew array from the document.

有人可以向我提供正确的查询吗?

Can someone please provide me the right query?

推荐答案

抱歉让你失望了,但你的答案

Sorry to disappoint you, but your answer

db.coll.update({
   _id:235399,
   "casts.crew.withBase": {$exists: true}
},{
   $unset: {
      "casts.crew.$.withBase" : true
   }
},false,true)

不正确.实际上它会删除该值,但仅从第一次出现的子文档开始,因为 位置运算符起作用:

is not correct. Actually it will remove the value, BUT only from the first occurrence of the subdocument, because of the way positional operator works:

位置 $ 运算符充当第一个元素的占位符匹配查询文档

the positional $ operator acts as a placeholder for the first element that matches the query document

您也不能使用 $unset (正如您之前尝试过的),因为它不能在数组上工作(并且您基本上是在尝试从数组中的文档中删除一个键).你也不能用 $pull 删除它,因为 pull 会删除所有数组,而不仅仅是它的一个字段.

You also can not use $unset (as you tried before) because it can not work on arrays (and are you basically trying to remove a key from a document from the array). You also can not remove it with $pull, because pull removes all the array, not just a field of it.

因此,据我所知,您无法使用简单的运算符来完成此操作.所以最后的方法是使用 $findforEach 保存.您可以在此处的答案中查看如何执行此操作.在您的情况下,您需要在 forEach 函数中使用另一个循环来遍历数组并删除一个键.我希望你能修改它.如果没有,我会尽力帮助您.

Therefore as far as I know you can not do this with a simple operator. So the last resort is doing $find and then forEach with save. You can see how to do this in my answer here. In your case you need to have another loop in forEach function to iterate through array and to delete a key. I hope that you will be able to modify it. If no, I will try to help you.

附言如果有人看起来有办法做到这一点 - 这是Sandra 的功能

P.S. If someone looks a way to do this - here is Sandra's function

db.coll.find({_id:235399}).forEach( function(doc) {
  var arr = doc.casts.crew;
  var length = arr.length;
  for (var i = 0; i < length; i++) {
    delete arr[i]["withBase"];
  }
  db.coll.save(doc);
});

这篇关于从mongodb数组中的所有元素中删除一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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