簡體   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