简体   繁体   English

findOneAndUpdate 嵌套数组中的嵌套 object

[英]findOneAndUpdate a nested object inside a nested array

I want to update a nested object inside another nested object in MongoDb that looks like this:我想在 MongoDb 中的另一个嵌套 object 中更新一个嵌套的 object,如下所示:

[
  {
    _id: ObjectId("637c648fb8fcfb2bc3071bb9"),
    consultant_name: "Sam letterman",
    consultantUsername: "sam",
    consultant_Password: "123",
    type: "consultant",
    clients: [
      {
        client_name: "john",
        client_Username: "mouh",
        client_Password: "123",
        type: "client",
        documents: [
          {
            name: "Acte de mariage",
            description: "copie conforme certifié du certificat de mariage",
            doc_upload: "",
            _id: ObjectId("637c648fb8fcfb2bc3071bbe")
          },
          {
            name: "Acte de de divorce",
            description: "because divorced",
            doc_upload: "",
            _id: ObjectId("637c648fb8fcfb2bc3071bbf")
          }
        ],
        
      },
      {
        _id: ObjectId("637c648fb8fcfb2bc3071bb9"),
        client_name: "narrakech",
        client_Username: "elsa",
        client_Password: "123",
        type: "client",
        documents: [
          {
            name: "Acte de mariage",
            description: "copie conforme certifié du certificat de mariage",
            doc_upload: "",
            _id: ObjectId("637c648fb8fcfb2bc3071bbe")
          },
          {
            name: "Acte de de divorce",
            description: "Parce que qu'on est shizo",
            doc_upload: "",
            _id: ObjectId("637c648fb8fcfb2bc3071bbf")
          }
        ]
      }
    ],
    createdAt: ISODate("2022-11-22T05:56:31.469Z"),
    updatedAt: ISODate("2022-11-27T03:40:31.835Z"),
    __v: 0
  }
]

More specifically, what I want to do is update the "doc_upload" of the client_Username "elsa".更具体地说,我想要做的是更新 client_Username“elsa”的“doc_upload”。 Here's my code这是我的代码

router.put('/:id', async (req, res) => {
    if (cond) {
        try {
            const updatedUser = await User.findOneAndUpdate({ 'clients._id': req.params.id }, {

                $set: { 'clients.$.client_name': 'narrakech' },


            }, { new: true });

            res.status(200).json(updatedUser);

        } catch (err) {
            res.status(500).json(err);
        }
    } else {
        res.status(401).json('You can update only you account.');
    }
})

Here I am able to update only the client_name (so one nested object), this is how far as is got.在这里,我只能更新 client_name(所以是一个嵌套对象),这就是目前的情况。 What I want to do is update one of her specific documents (the doc_upload section for example), the idea behind all this is to have access to the second array (documents) which I can't do for now using the $ sign.我想要做的是更新她的一个特定文档(例如 doc_upload 部分),所有这一切背后的想法是访问第二个数组(文档),我现在不能使用 $ 符号。

Is there any way to do this in MongoDb? MongoDb有没有办法做到这一点?

Thank you谢谢

Yes there is a way to do this, in fact several ways.是的,有一种方法可以做到这一点,实际上有几种方法。

The simplest will be to use the array filters option, like so:最简单的方法是使用数组过滤器选项,如下所示:

db.collection.findOneAndUpdate({
  "clients._id": req.params.id
},
{
  $set: {
    "clients.$.client_name": "narrakech",
    "clients.$.documents.$[doc].doc_upload": "new doc upload"
  }
},
{
  arrayFilters: [
    {
      "doc._id": subDocumentId // this is the "query" to match the sub document in the documents array.
    }
  ]
})

Mongo Playground蒙戈游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM