为什么更新在猫鼬中不起作用但是在猫鼬中很好

时间:2020-09-12 14:44:16

标签: node.js mongodb mongoose

我正在更新我的餐馆集合嵌套数组。 我想更新bigTables collection,我确实在MongoDB中进行了更新,但是猫鼬不起作用。

db.restaurant.find({})

        "restaurant_tag" : "korean",
        "restaurant_location" : "",
        "restaurant_owner" : ObjectId("5f2a1541fe39032baca6809e"),
        "bigTables" : [
                [
                        {
                                "reserved" : false,
                                "_id" : ObjectId("5f5ca351e2e67f71d4ad8345"),
                                "beUsing" : true,
                                "orderTime" : "0",
                                "menu" : "Not yet",
                                "__v" : 0
                        },
                        {
                                "reserved" : false,
                                "_id" : ObjectId("5f5ca351e2e67f71d4ad8346"),
                                "beUsing" : true,
                                "orderTime" : "0",
                                "menu" : "Not yet",
                                "__v" : 0
                        },
                        {
                                "reserved" : false,
                                "_id" : ObjectId("5f5ca351e2e67f71d4ad8347"),
                                "beUsing" : true,
                                "orderTime" : "0",
                                "menu" : "Not yet",
                                "__v" : 0
                        },
                        {
                                "reserved" : false,
                                "_id" : ObjectId("5f5ca351e2e67f71d4ad8348"),
                                "beUsing" : true,
                                "orderTime" : "0",
                                "menu" : "Not yet",
                                "__v" : 0
                        }
                ],
                [
                        {
                                "reserved" : false,
                                "_id" : ObjectId("5f5ca351e2e67f71d4ad8349"),
                                "beUsing" : true,
                                "orderTime" : "0",
                                "menu" : "Not yet",
                                "__v" : 0
                        },
                        {
                                "reserved" : false,
                                "_id" : ObjectId("5f5ca351e2e67f71d4ad834a"),
                                "beUsing" : true,
                                "orderTime" : "0",
                                "menu" : "Not yet",
                                "__v" : 0
                        },
                        {
                                "reserved" : false,
                                "_id" : ObjectId("5f5ca351e2e67f71d4ad834b"),
                                "beUsing" : true,
                                "orderTime" : "0",
                                "menu" : "Not yet",
                                "__v" : 0
                        },
                        {
                                "reserved" : false,
                                "_id" : ObjectId("5f5ca351e2e67f71d4ad834c"),
                                "beUsing" : true,
                                "orderTime" : "0",
                                "menu" : "Not yet",
                                "__v" : 0
                        }
                ]
        ],
}

我想更新bigTables的保留,使用,orderTime和菜单。

    try {
      const restaurant = await Restaurant.updateMany(
        { _id: req.body.id },
        {
          $set: {
            "bigTables.$[row].$[].orderTime": req.body.createAt,
          },
        },
        {
          arrayFilters: [
            { "row._id": mongoose.mongo.ObjectID(req.body.tableId) },
          ],
        },
        function (err, user) {
          if (err) return handleError(err);
          res.send(user);
        }
      );

      console.log(restaurant);
    } catch (error) {
      console.log(error);
    }

--- result ---
{ n: 1, nModified: 0, ok: 1 }

看起来工作不正常。我认为它是匹配的但未修改... 这让我生气

req.body
{
  id: '5f5ca351e2e67f71d4ad834f',
  tableId: '5f5ca351e2e67f71d4ad8346',
  menu: { '정원김밥': '1' },
  createAt: '20200906:2327',
  price: { '0': '1500', '1': '1500' },
  table: 'bigTable'
}

这是mongoDB。如您所见,它正常工作

> db.restaurants.updateOne(
   { _id : ObjectId("5f5ca3e4e2e67f71d4ad835d") }, 
   {$set : {"bigTables.$[row].$[].orderTime" : "12:00" } },
   { arrayFilters : [ {"row._id":ObjectId("5f5ca3e4e2e67f71d4ad8351")} ]} )


{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

请帮助我thanxs 我一直在等待您的帮助。

0 个答案:

没有答案