简体   繁体   English

如何从深度嵌套的文档中 $pull (mongoose)

[英]How To $pull From Deeply Nested Document (mongoose)

I need to delete a location object from the locations array.我需要从位置数组中删除位置 object。 It is deeply nested.它嵌套很深。 I followed mongoose documentation but my attempts didn't work:我遵循了 mongoose 文档,但我的尝试没有奏效:

 lists = [{ "listName": "Test", "_id": "8d55f0afe545a0178c320706", "listId": "5fd9a3bef6c39b2f9c4df65b", "date": "12/15/2020", "dueDate": "2020-11-18", "items": [ { "itemNumber": 123, "description": "item123", "onHand": 60, "_id": "13dd1f26ecd2baeb61b3b455", "locations": [ { "locationName": "loc1", "count": 10, "_id": "50a2c969465ba8010bd48977" }, { "locationName": "loc2", "count": 20, "_id": "51c2f1d25311dc8fabdbf604a59b" }, { "locationName": "Loc3", "count": 30, "_id": "7cb0c1f51a91c384846d65f8b2ae" } ] }, {more lists}

Attempt:试图:

 router.post("/lists/deleteLoc", (req, res) => { const { listId, list_id, item_id, location_id } = req.body; List.updateOne({ "lists.listId": listId, "lists._id": list_id }, { $pull: { "lists.$.items": { locations: { $elemMatch: { _id: location_id }).then(() => res.json({ msg: "location removed" })).catch((err) => res.status(400).json({ msg: "Error: " + err })); });

If the request location_id was "7cb0c1f51a91c384846d65f8b2ae" it should delete the last location from the array.如果请求 location_id 是“7cb0c1f51a91c384846d65f8b2ae”,它应该从数组中删除最后一个位置。 The desired result:期望的结果:

 lists = [{ "listName": "Test", "_id": "8d55f0afe545a0178c320706", "listId": "5fd9a3bef6c39b2f9c4df65b", "date": "12/15/2020", "dueDate": "2020-11-18", "items": [ { "itemNumber": 123, "description": "item123", "onHand": 60, "_id": "13dd1f26ecd2baeb61b3b455", "locations": [ { "locationName": "loc1", "count": 10, "_id": "50a2c969465ba8010bd48977" }, { "locationName": "loc2", "count": 20, "_id": "51c2f1d25311dc8fabdbf604a59b" } ] }, {more lists}

I've tried basically all variations of this, but none have worked.我已经尝试了基本上所有的变体,但没有一个奏效。

I'm also not sure if making a router.post or an axios.post request for deletion is correct.我也不确定发出 router.post 或 axios.post 删除请求是否正确。 Should this be axios.delete and router.delete?这应该是 axios.delete 和 router.delete 吗?

I've tried this in one of my similar DB and worked!我已经在我的一个类似数据库中尝试过这个并且工作了!

List.updateOne({ "listId": yourListId },
        {
            '$pull': {
                'items.$[item].locations': { "_id": yourLocationId }
            }
        }, {
        "arrayFilters": [
            {
                "item._id": yourItemId
            }
        ]
    }, function (err) {
        if (err) {
            res.json(err)
        } else {
            res.json({ message: "Updated" })
        }
    })
}

You've to put the values that're inside your DB from the object that you want to delete.您必须将要删除的 object 中的值放入数据库中。

So if you want to delete the object with所以如果你想删除 object

"locationname": "Loc3" “位置名称”:“Loc3”

You should use你应该使用

var yourListId = "5fd9a3bef6c39b2f9c4df65b";
var yourItemId = "13dd1f26ecd2baeb61b3b455";
var yourLocationId = "7cb0c1f51a91c384846d65f8b2ae";

Try it out!试试看!

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

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