代码之家  ›  专栏  ›  技术社区  ›  Junayy

如何将嵌入的文档更新到嵌套数组中?

  •  0
  • Junayy  · 技术社区  · 6 年前

    我在Mongo系列中采用了这种结构:

    {
      "_id": "12345678",
      "Invoices": [
        {
          "_id": "123456789",
          "Currency": "EUR",
          "DueTotalAmountInvoice": 768.3699999999999,
          "InvoiceDate": "2016-01-01 00:00:00.000",
          "Items": [
            {
              "Item": 10,
              "ProductCode": "ABC567",
              "Quantity": 1
            },
            {
              "Item": 20,
              "ProductCode": "CDE987",
              "Quantity": 1
            }
          ]
        },
        {
          "_id": "87654321",
          "Currency": "EUR",
          "DueTotalAmountInvoice": 768.3699999999999,
          "InvoiceDate": "2016-01-01 00:00:00.000",
          "Items": [
            {
              "Item": 30,
              "ProductCode": "PLO987",
              "Quantity": 1,
              "Units": "KM3"
            },
            {
              "Item": 40,
              "ProductCode": "PLS567",
              "Quantity": 1,
              "DueTotalAmountInvoice": 768.3699999999999
            }
          ]
        }
      ]
    }
    

    因此,我有一个第一个对象存储几个发票,每个发票存储几个项目。项目是嵌入的文档。 因此,在关系建模中: 客户有一张或多张发票 一张发票有一个或多个项目

    我面临一个问题,因为我正在尝试将特定项目更新为特定发票。例如,我想更改发票123456789中项目10的数量。

    在Mongodb中如何做到这一点?

    我试过:

    • Push语句,但它似乎不适用于嵌套数组

    • arrayFilters,但它似乎不适用于嵌套数组中的嵌入式文档(仅适用于简单的值数组)。

    你能给我一些建议吗?

    非常感谢。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Rahul Raj    6 年前

    根据您在此处的问题描述:

    For example I want to change the quantity of the item 10 in Invoice 123456789. 我刚换了 Quantity 至3。您可以根据需要在此处执行任何操作。你只需要记下我是如何 arrayFilters 在这里

    尝试此查询:

    db.collection.update(
     {"_id" : "12345678"},
     {$set:{"Invoices.$[element1].Items.$[element2].Quantity":3}},
     {multi:true, arrayFilters:[ {"element1._id": "123456789"},{ 
      "element2.Item": { $eq: 10 }} ]}
    )
    

    从mongo shell(mongo 3.6.3)成功执行上述查询。我看到了这个结果:

    /* 1 */
    {
    "_id" : "12345678",
    "Invoices" : [ 
        {
            "_id" : "123456789",
            "Currency" : "EUR",
            "DueTotalAmountInvoice" : 768.37,
            "InvoiceDate" : "2016-01-01 00:00:00.000",
            "Items" : [ 
                {
                    "Item" : 10,
                    "ProductCode" : "ABC567",
                    "Quantity" : 3.0
                }, 
                {
                    "Item" : 20,
                    "ProductCode" : "CDE987",
                    "Quantity" : 1
                }
            ]
        }, 
        {
            "_id" : "87654321",
            "Currency" : "EUR",
            "DueTotalAmountInvoice" : 768.37,
            "InvoiceDate" : "2016-01-01 00:00:00.000",
            "Items" : [ 
                {
                    "Item" : 30,
                    "ProductCode" : "PLO987",
                    "Quantity" : 1,
                    "Units" : "KM3"
                }, 
                {
                    "Item" : 40,
                    "ProductCode" : "PLS567",
                    "Quantity" : 1,
                    "DueTotalAmountInvoice" : 768.37
                }
            ]
        }
     ]
    }
    

    enter image description here

    这就是你想要的吗?