如何从Mongo深层嵌套文档中的数组中提取值

时间:2016-04-29 18:12:49

标签: mongodb

我在MongoDB 2.6中有以下结构:

if(size(left) > size(right)) {
    countResult.reserve(size(left));
    transform(cbegin(right), cend(right), cbegin(left), begin(countResult), plus<int>());
    countResult.insert(end(countResult), next(cbegin(left), size(right)), cend(left));
} else {
    countResult.reserve(size(right));
    transform(cbegin(left), cend(left), cbegin(right), becing(countResult), plus<int>());
    countResult.insert(end(countResult), next(cbegin(right), size(left)), cend(right));
}

我想从所有数组db.processes: { _id: ObjectId("572756344031b585d03dca57"), activities: [ { procedures: [ { implications_ids: [ ObjectId("50871f11d52ef600020128a2"), ObjectId("50871f17d52ef60002012d03") ] } ] } ] } 中删除值ObjectId("50871f11d52ef600020128a2")。我写了命令:

implications_ids

命令完成执行,但不更新任何文档:

`db.processes.update({}, { $pull: { activities: { procedures: { implications_ids: ObjectId("50871f11d52ef600020128a2") } } } }, { multi: true, safe: true } )`

我做错了什么?

1 个答案:

答案 0 :(得分:0)

试过这个。我在mongo3.2中测试过,所以不确定它是否在2.6

中运行良好
db.foo.insert( { activities: {
      procedures: {
        implications_ids: [ ObjectId("50871f11d52ef600020128a2"),
                          ObjectId("50871f17d52ef60002012d03")]
      }
    }
  });

我用

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.foo.find()

{ "_id" : ObjectId("5723a45f0a8d4f8f305690e6"), "activities" : { "procedures" :
{ "implications_ids" : [ ObjectId("50871f17d52ef60002012d03") ] } } }
>

得到

{{1}}

执行命令后,只需更改文档名称

即可