更新猫鼬嵌套数组 - 的MongoDB [英] Update nested array with Mongoose - MongoDB

查看:136
本文介绍了更新猫鼬嵌套数组 - 的MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新嵌套数组中的值,但不能让它开始工作。

I am trying to update a value in the nested array but can't get it to work.

我的目标是这样的。

 {
    "_id": {
        "$oid": "1"
    },
    "array1": [
        {
            "_id": "12",
            "array2": [
                  {
                      "_id": "123",
                      "answeredBy": [],
                  },
                  {
                      "_id": "124",
                      "answeredBy": [],
                  }
             ],
         }
     ]
 }

我要推的值改为作答,数组。

I need to push a value to "answeredBy" array.

在下面的例子中,我试着推成功字符串的123 _id对象作答,数组,但它不工作。

In the below example, I tried pushing "success" string to the "answeredBy" array of the "123 _id" object but it does not work.

callback = function(err,value){
     if(err){
         res.send(err);
     }else{
         res.send(value);
     }
};
conditions = {
    "_id": 1,
    "array1._id": 12,
    "array2._id": 123
  };
updates = {
   $push: {
     "array2.$.answeredBy": "success"
   }
};
options = {
  upsert: true
};
Model.update(conditions, updates, options, callback);

我发现这个链接,但其只回答说,我应该使用对象像结构,而不是阵列。这不能在我的处境得到应用。我真的需要嵌套在阵列我的对象

I found this link, but its answer only says I should use object like structure instead of array's. This cannot be applied in my situation. I really need my object to be nested in arrays

这将是巨大的,如果你能帮助我在这里。我已经花时间摸不着头脑。

It would be great if you can help me out here. I've been spending hours to figure this out.

感谢你在前进!

推荐答案

有几件事情错了,你在这里做什么。首先你的查询条件。你指的是几个 _id ,你不应该需要,并且其中至少有一个是不是在顶层值。

There are a few things wrong with what you are doing here. Firstly your query conditions. You are referring to several _id values where you should not need to, and at least one of which is not on the top level.

为了进入一个嵌套的价值,也presuming的 _id 值是唯一的,不会出现在任何其他文件中,您查询表格应是这样的:

In order to get into a "nested" value and also presuming that _id value is unique and would not appear in any other document, you query form should be like this:

Model.update(
    { "array1.array2._id": "123" },
    { "$push": { "array1.0.array2.$.answeredBy": "success" } },
    function(err,numAffected) {
       // something with the result in here
    }
);

现在,将实际的工作,但实际上它只是一个侥幸,它不会因为有很好的理由,为什么它不应该为你工作。

Now that would actually work, but really it is only a fluke that it does as there are very good reasons why it should not work for you.

重要的读数为位置 $ 嵌套阵列​​的主题下,运营商。它说的是:

The important reading is in the official documentation for the positional $ operator under the subject of "Nested Arrays". What this says is:

的位置$操作者不能用于其中跨越多个阵列的查询,诸如遍历嵌套在其它的阵列中的阵列的查询,因为更换为$占位符是一个单一的值

The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value

具体是什么也就意味着,将被匹配,并在位置占位符返回的元素从第一匹配数组的索引值。这意味着在你的情况下,顶级阵列上的匹配指标。

Specifically what that means is the element that will be matched and returned in the positional placeholder is the value of the index from the first matching array. This means in your case the matching index on the "top" level array.

所以,如果你看到如图所示的查询符号,我们有硬codeD在第一(或0指数)顶级数组中的位置,它只是恰巧,在数组2匹配的元素也是零索引条目。

So if you look at the query notation as shown, we have "hardcoded" the first ( or 0 index ) position in the top level array, and it just so happens that the matching element within "array2" is also the zero index entry.

为了证明这一点,你可以改变的匹配 _id 值设置为124,结果将 $推的新进​​入到元素与 _id 123,因为他们都在ARRAY1的零索引条目,这是返回的值的占位符。

To demonstrate this you can change the matching _id value to "124" and the result will $push an new entry onto the element with _id "123" as they are both in the zero index entry of "array1" and that is the value returned to the placeholder.

所以这是嵌套数组的一般问题。你可以删除其中的一个级别,你仍然能够 $推来正确的元素在你的顶级数组,但仍然会有多个级别。

So that is the general problem with nesting arrays. You could remove one of the levels and you would still be able to $push to the correct element in your "top" array, but there would still be multiple levels.

尽量避免嵌套数组作为如图所示,你将遇到更新问题。

Try to avoid nesting arrays as you will run into update problems as is shown.

这篇关于更新猫鼬嵌套数组 - 的MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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