繁体   English   中英

如何删除 mongodb 集合中数组内的 object?

[英]How to delete object inside an array in mongodb collection?

我有一个 controller function 作为:

User.updateOne(
    { _id: "6252d87e010b3f94adcf4e41" },
            {
                $pull: {
                    newCart: {
                        "post._id": "6249e216c1e3b6ae2d4aa273",
                    },
                },
            },
            {new:true}
        ).then((res) => console.log(res, user));

响应是这样的:

{
  acknowledged: true,
  modifiedCount: 1,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}

它显示 modifiedCount 为 1,但在我的实时数据库集合中从未发生变化。

我的收藏看起来像这样:我的数据库收藏的图片

查询是正确的也许尝试添加 exec

User.updateOne(
  { _id: "6252d87e010b3f94adcf4e41" },
  {
    $pull: {
      newCart: {
        "post._id": "6249e216c1e3b6ae2d4aa273",
      },
    },
  },
  { new: true }
)
  .exec()
  .then((res) => console.log(res, user));

希望这能解决问题。

您的查询完全没问题。 断开您的数据库并尝试再次连接。 重启后也许这会工作

暂无
暂无

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

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