使用initializeOrderedBulkOp更新嵌套数组

时间:2018-10-08 07:49:39

标签: mongodb mongodb-query aggregation-framework

尝试在应用程序数组内插入或更新bill_info数据。 我正在为搜索部门使用四个输入参数,sub_dept,application.name和类型。使用initializeOrderedBulkOp及以下内容是mongo查询

bulk.find({ $and : [{ "department_name" : "Finance" , 
  "sub_dept":{$elemMatch: {name: "Direct" }}, 
  "sub_dept.application": {$elemMatch: {name:"HRM"}} , "type" : "Online" }]}).update
   ({ $addToSet : { "sub_dept.$.application.0.bill_info" : 
     { "items" : [{ "qty" : "860000",  "cost" : 390}],
        "invoice" : 1000}
  }});

我的要求是根据匹配的四个参数输入(部门,sub_dept,application.name和类型)更新任何应用程序阵列中的账单信息。上面的查询有时可以运行,但并非在所有情况下都可以。

{
  "_id": ObjectId("5bba5d918169541704d38a6e"),
  "department": "Finance",
  "sub_dept": [
    {
      "name": "Indirect",
      "application": [
        {
          "name": "Cab",
          "date": "25/10/2013",
          "cp": "Sam",
          "bill_info": [
            {
              "items": [
                {
                  "qty": "32",
                  "cost": NumberInt(22300)
                },
                {
                  "qty": "6000",
                  "cost": NumberInt(390)
                }
              ],
              "invoice": "1000",

            }
          ]
        },
        {
          "name": "Bus",
          "date": "25/10/2013",
          "cp": "John"
        }
      ]
    },
    {
      "name": "Direct",
      "application": [
        {
          "name": "HRM",
          "date": "25/10/2013",
          "cp": "Alen"
        },
        {
          "name": "Showroom",
          "date": "25/10/2013",
          "cp": "Xavier""bill_info": [
            {
              "items": [
                {
                  "qty": "32",
                  "cost": NumberInt(22300)
                },
                {
                  "qty": "6000",
                  "cost": NumberInt(390)
                }
              ],
              "invoice": "2000",

            }
          ]
        }
      ]
    },
    {
      "name": "Hybrid",
      "application": [
        {
          "name": "Telephone",
          "date": "25/10/2013",
          "cp": "Galn""bill_info": [
            {
              "items": [
                {
                  "qty": "32",
                  "cost": NumberInt(22300)
                },
                {
                  "qty": "6000",
                  "cost": NumberInt(390)
                }
              ],
              "invoice": "3000",

            }
          ]
        },
        {
          "name": "Mobile",
          "date": "25/10/2013",
          "cp": "Paul"
        }
      ]
    }
  ],
  "type": "online"
}

对此的任何帮助都会非常有帮助

0 个答案:

没有答案