【问题标题】:How to update an embedded document into a nested array?如何将嵌入式文档更新为嵌套数组?
【发布时间】:2018-09-20 03:04:09
【问题描述】:

我有这种结构成 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
        }
      ]
    }
  ]
}

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

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

如何在 Mongodb 中做到这一点?

我试过了:

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

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

你能给我一些建议吗?

谢谢!

【问题讨论】:

  • 你的 mongo 服务器版本是多少?运行 db.version() 以了解版本。你能用数组过滤器显示查询吗?
  • @Junayy, arrayFilters 将无法正常工作,除非您从最新的 mongo shell 执行查询。声明 I tried arrayFilters but it doesn't seem to work for embedded document in nested arrays 也不是真的。
  • @Veeram 我在 mongo 3.6
  • 你是在 mongo shell 还是第三方工具中执行?
  • @Rahul Raj mongo shell

标签: arrays mongodb mongodb-query


【解决方案1】:

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

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
            }
        ]
    }
 ]
}

这就是你想要的吗?

【讨论】:

    猜你喜欢
    • 2015-07-13
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2012-05-11
    相关资源
    最近更新 更多