【问题标题】:findOneAndUpdate for nested object elements of an array with two conditionsfindOneAndUpdate 用于具有两个条件的数组的嵌套对象元素
【发布时间】:2020-02-01 10:04:18
【问题描述】:

我想将“displayTitle”更新为“12345”,其中 objectId 为“5d8c8f4a4449a851f6kkk6o2”在 topten=“under 50000”的“标题”下 findOneAndUpdate 用于具有两个条件的数组的嵌套对象元素,首先用于选择特定文档和第二个是在嵌套数组中查找特定对象。集合名称是:topten。

> db.topten.find().pretty();
{
    "_id" : ObjectId("5d8c8f254449a851f6aaa6r3"),
    "title" : "under 50000",
    "products" : [
        {
            "_id" : ObjectId("5d8c8f4a4449a851f6kkk6o2"),
            "productId" : "5cfe541ae759d9699b5213542",
            "displayTitle" : "dhtyks",
            "score" : 100,
            "priority" : 50,
            "description" : "Upcoming"
        },
        {
            "_id" : ObjectId("5d8c8f8f4449a86544646"),
            "productId" : "5cfe512bca044365efsdfgg45",
            "displayTitle" : "Spice",
            "score" : 10,
            "priority" : 800,
            "description" : "Advanced"
        }
    ],
    "created" : ISODate("2019-09-26T10:12:53.804Z"),
    "updated" : ISODate("2019-10-03T05:24:19.296Z"),
    "slug" : "under-10000",
    "__v" : 0
{
    "_id" : ObjectId("5d8c8f254449a851f6ddd9a1"),
    "title" : "under 10000",
    "products" : [
        {
            "_id" : ObjectId("5d8c8f4a4449a851f6ddd9a2"),
            "productId" : "5cfe541ae759d9699b56759b",
            "displayTitle" : "ABCDEF6",
            "score" : 10,
            "priority" : 200,
            "description" : "New Gen"
        },
        {
            "_id" : ObjectId("5d8c8f8f4449a851f6ddd9a4"),
            "productId" : "5cfe512bca044365ef1dcd0f",
            "displayTitle" : "Micromax",
            "score" : 1,
            "priority" : 200,
            "description" : "Upcoming"
        }
    ],
    "created" : ISODate("2019-09-26T10:12:53.804Z"),
    "updated" : ISODate("2019-10-03T05:24:19.296Z"),
    "slug" : "under-10000",
    "__v" : 0

我使用的代码是:

topten.findOneAndUpdate({"title" : "under 50000",  "products": { $elemMatch: { "_id": "5d8c8f4a4449a851f6kkk6o2" } }},
                    {"products.0.displayTitle":"12345"},
                        function (err, doc) {
                        if (err) {
                            console.log(err);
                             reject(new Error(err));
                            return;
                        } else {
                            console.log('AAAAAAAAAAAAAAAAAAAAAAAA', doc)
                                                       res.status(200)
                                .json({
                                    success: true,
                                    msg: 'Attached product updated succesfully`);'
                                })

                        }
                    }
                )

查询正在运行,但它没有更新我的 Db 中的值。请帮忙,我卡在这里了。

【问题讨论】:

    标签: mongoose


    【解决方案1】:

    尝试使用 $set 来分配 products.0.displayTitle,如下所示:

       topten.findOneAndUpdate(
            {"title" : "under 50000",  
            "products": { $elemMatch: { "_id": "5d8c8f4a4449a851f6kkk6o2" } }},
              {$set: { products.0.displayTitle: "12345"}},
              function (err, doc) {
              if (err) {
                  console.log(err);
                   reject(new Error(err));
                  return;
              } else {
                  console.log('AAAAAAAAAAAAAAAAAAAAAAAA', doc)
                                             res.status(200)
                      .json({
                          success: true,
                          msg: 'Attached product updated succesfully`);'
                      })
    
              }
          }
      )
    

    【讨论】:

    • 感谢您的建议@Michael Gervasoni {$set: { products.0.displayTitle: "12345"}},其他一切都一样,并且有效。再次感谢。
    • 谢谢! @迈克尔·格瓦索尼。使用 $set 对我有用。
    猜你喜欢
    • 2019-12-30
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2018-05-27
    • 2021-01-14
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多