从MongoDB中的对象数组中删除一个项

bvjveswy  于 2023-08-04  发布在  Go
关注(0)|答案(1)|浏览(109)

我是相当新的mongodb,所以如果我的问题不相关,请忽略它。
我在MongoDB中有一个文档如下。我试图从文件数组中删除和项。不是第一个或最后一个。匹配查询的。这就是为什么我谷歌了很多,但无法找到解决方案。谁能帮我解决这个问题?
目前为止我所尝试的:

db.CustomerRequest.updateOne({
    "_id": ObjectId("64b7787dbe120dde9b70697a"),
    "proposal.id": "f197a152-26d1-465c-b533-7340c308a06d"
},
    {
        "$pull":
        {
            "proposal": [{ "files": [{ "src": "5989ace4-1355-4af6-bef6-0b38cf906209/PNG Photo.png" }] }]

        }
    }
)

个字符
我总是得到如下的回答。因此,未删除数组项。
{“已确认”:true,“matchedCount”:1,“modifiedCount”:0 }

JSON示例

{
    "_id" : ObjectId("64b7787dbe120dde9b70697a"),
    "reqId" : "mysterious-monkey-39",
    "userName" : " ",
    "userId" : "5989ace4-1355-4af6-bef6-0b38cf906209",
    "mode" : 1,
    "type" : 1,
    "createDate" : ISODate("2023-07-19T05:45:33.314Z"),
    "autoCloseDate" : ISODate("2023-07-26T05:45:33.327Z"),
    "itemCat" : "Mice",
    "itemName" : "fasfdsadf dfsdfs",
    "zipCode" : " ",
    "quantity" : 1,
    "unit" : "piece(s)",
    "budget" : 0,
    "currency" : " ",
    "condition" : " ",
    "country" : " ",
    "state" : "Idaho",
    "urgency" : 1,
    "attachment" : [ ],
    "isClosed" : false,
    "detail" : "",
    "proposal" : [
        {
            "id" : "f197a152-26d1-465c-b533-7340c308a06d",
            "productName" : "asdASD",
            "quote" : 1000000,
            "promo" : "SDSFASD",
            "warranty" : "No",
            "warrantyPeriod" : null,
            "condition" : "Brand new",
            "freeShipping" : "No",
            "createDate" : ISODate("2023-07-20T18:34:29.788Z"),
            "organizationId" : ObjectId("61d7dcfe9beab44d14202b86"),
            "userId" : "5989ace4-1355-4af6-bef6-0b38cf906209",
            "isActive" : true,
            "description" : "",
            *"files" : [
                {
                    "src" : "5989ace4-1355-4af6-bef6-0b38cf906209/PNG Photo.png",
                    "width" : 1500,
                    "height" : 500
                },
                {
                    "src" : "5989ace4-1355-4af6-bef6-0b38cf906209/PNG Photo_Vertical.png",
                    "width" : 500,
                    "height" : 1500
                },
                {
                    "src" : "5989ace4-1355-4af6-bef6-0b38cf906209/JPG Photo.jpg",
                    "width" : 1500,
                    "height" : 500
                },
                {
                    "src" : "5989ace4-1355-4af6-bef6-0b38cf906209/JPG Photo_Vertical.jpg",
                    "width" : 500,
                    "height" : 1500
                }
            ]*
        }
    ]
}


我已经写在问题描述里了

oyxsuwqo

oyxsuwqo1#

移除巢状files数组中的元素。

db.collection.update({
  _id: ObjectId("64b7787dbe120dde9b70697a"),
  "proposal.id": "f197a152-26d1-465c-b533-7340c308a06d"
},
{
  $pull: {
    "proposal.$[].files": {
      "src": "5989ace4-1355-4af6-bef6-0b38cf906209/PNG Photo.png"
    }
  }
})

字符串
mongoplayground

相关问题