Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如果外部数组中的id不匹配,则推入外部数组;如果外部数组中的id匹配,则推入内部数组_Node.js_Mongodb - Fatal编程技术网

Node.js 如果外部数组中的id不匹配,则推入外部数组;如果外部数组中的id匹配,则推入内部数组

Node.js 如果外部数组中的id不匹配,则推入外部数组;如果外部数组中的id匹配,则推入内部数组,node.js,mongodb,Node.js,Mongodb,我有这样的模型 { "array1": [ { "_id": "123", "array2": [ { "_id": "123", "answeredBy": [],

我有这样的模型

     {
     "array1": [
                {
                    "_id": "123",
                    "array2": [
                          {
                              "_id": "123",
                              "answeredBy": [],
                          }

                     ],
                 }
             ] 
}
如果我在array1中有id,请将文档推入array2或在array1中创建新文档

例如:

如果我给你一个新的目标

{
   "_id": "456",
   "answeredBy": [],
} 
如果给出数组1的id为123

结果应该是

      {  
   "array1":[  
      {  
         "_id":"123",
         "array2":[  
            {  
               "_id":"123",
               "answeredBy":[  

               ],

            },
            {  
               "_id":"456",
               "answeredBy":[  

               ],

            }
         ],

      }
否则,如果给出阵列1的id,而不是123,即不存在,即657

结果是

{  
   "array1":[  
      {  
         "_id":"123",
         "array2":[  
            {  
               "_id":"123",
               "answeredBy":[  

               ],

            },

         ],

      }      {  
         "_id":"657",
         "array2":[  
            {  
               "_id":"456",
               "answeredBy":[  

               ],

            }
         ],

      }
结果应该是

      {  
   "array1":[  
      {  
         "_id":"123",
         "array2":[  
            {  
               "_id":"123",
               "answeredBy":[  

               ],

            },
            {  
               "_id":"456",
               "answeredBy":[  

               ],

            }
         ],

      }
我如何才能做到这一点?

您的问题是,除非您同时推送两个更新

不可能以原子方式执行请求,因此需要两个更新查询,每个请求一个

当找到匹配的外部记录时更新,这将通过在内部数组中推送新元素来进行更新

db.col.update(
    {"array1._id": "123"},
    {"$push":{"array1.$.array2":{"_id":"456","answeredBy":[]}}}
)
更新当外部记录不匹配时,您将获得0的修改计数

db.col.update(
    {"array1._id": "657"},
    {"$push":{"array1.$.array2":{"_id":"456","answeredBy":[]}}}
)

if(nModified == 0) {
  db.col.update(
    {},
    {"$push":{"array1":{ "_id":"657","array2":[{"_id":"456","answeredBy":[]}}}}
  )
}

您可以阅读链接答案以了解更多详细信息。

我不能在单个查询中执行此操作吗?不,您不能在嵌套数组中执行此操作。upsert在文档级别工作谢谢。我试图在一个查询中完成这项工作,这可能会减少我的应用程序中的一个db命中。