如何将嵌入式文档更新为嵌套数组? [英] How to update an embedded document into a nested array?

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

问题描述

我将这种结构放入Mongo集合中:

I have this kind of structure into a Mongo collection :

{
  "_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张或多张发票 发票中有1个或多个项目

So I have a first object storing several Invoices and each Invoice is storing several Items. An item is an embedded document. So in relational modelisation : A customer has 1 or several Invoice An Invoice has 1 or several Item

由于我正尝试将特定项目更新为特定发票,因此我遇到了一个问题.例如,我想更改发票123456789中项目10的数量.

I am facing an issue since I am trying to update a specific Item into a specific a specific Invoice. For example I want to change the quantity of the item 10 in Invoice 123456789.

在Mongodb中如何做到这一点?

How is it possible to do that in Mongodb ?

我尝试过:

  • 推送语句,但似乎不适用于嵌套数组

  • Push statement but it doesn't seem to work for nested arrays

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

arrayFilters but it doesn't seem to work for embedded document in nested arrays (only simple value arrays).

您能给我一些建议吗?

谢谢!

推荐答案

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

As per your problem description here:

For example I want to change the quantity of the item 10 in Invoice 123456789.我刚刚将Quantity更改为3.您可以根据需要在此处执行任何操作.您只需要在这里记下我如何使用arrayFilters即可.

For example I want to change the quantity of the item 10 in Invoice 123456789. I just changed the Quantity to 3. You can perform any operations here as you want. You just need to take note of how I used arrayFilters here.

尝试此查询:

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)成功执行.我看到了这个结果:

The above query successfully executed from mongo shell (Mongo 3.6.3). And I see this result:

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

这就是您想要的吗?

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

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