MongoDB更新嵌套数组 [英] MongoDB Update a Nested Array

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

问题描述

我有以下JSON:

{
"_id" : ObjectId("57ce1a55899bf934e59edd0d"),
"project_name" : "Coupletones",
"list_users" : [
    "testmail"
],
"iterations" : [
    {
        "iteration_name" : "Iteration1",
        "tasks" : [ ]
    },
    {
        "iteration_name" : "Iteration2",
        "tasks" : [ ]
    },
]
}

我希望能够将其放入与迭代2相关联的任务数组中.如何正确查询并插入到正确的位置?到目前为止,这就是我所拥有的,但是它总是插入到与迭代1相关联的task数组中.

I want to be able to push things into to the tasks array associated with Iteration 2. How do I properly query and insert to the correct location? This is what I have so far, but it always inserts into the tasks array associated with Iteration 1.

var ans = collection_projects.update({
      "project_name" : project_name,
      "list_users" : email,
      "iterations.iteration_name": iteration_name,
      },

      {$addToSet: {"iterations.$.tasks": {
        task_name: task_name,
        task_description : task_description,
        task_assignee: task_assignee,
        task_status : -1 } } }
    );

我已经看到了: MongoDB嵌套数组查询 但是他只想推送到一个嵌套数组.

I've seen this: MongoDB nested array query but he is only trying to push to one nested array.

推荐答案

通过此方法尝试

conditions = {
    "_id": ObjectId"57ce1a55899bf934e59edd0d",
    "iterations.iterations_name": "Iteration2"
  };
updates = {
   $push: {
     "iterations.$.tasks": "success"
   }
};
options = {
  upsert: true
};
Model.update(conditions, updates, options, callback);

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

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