保持嵌套数组,但在mongodb

时间:2017-11-05 01:59:24

标签: node.js mongodb

构建一个Nodejs应用程序,我试图从Mongo数据库中提取所有双重嵌套记录。尝试我只删除了一个双重嵌套记录或所有嵌套记录。如下面的示例数据,我一直在尝试删除具有相同keyId的所有票证。我已经减少了示例,但是作为数组的票证可能存在具有相同结构的其他元素,具有不同的" keyIds"不应该被删除。我查看this问题,但它只是删除了一个双嵌套数组的记录,而不是一次全部删除。

[
    {
        "_id": "59fe54098448d822f89a7e62",
        "ownerId": "59b23449b20b7c1838eee1a3",
        "name": "Home",
        "keys": [
            {
                "id": "6d7435625564594f4a563554796c6a77",
                "name": "Front Door"
            }
        ],
        "grants": [
            {
                "id": "307658775634774a556b677650347072",
                "userId": "59b23449b20b7c1838eee1a3",
                "tickets": [
                    {
                        "keyId": "6d7435625564594f4a563554796c6a77",
                        "iv": "b7090268bdaf9ab55270e133b5629e28"
                    }
                ]
            },
            {
                "id": "37703369365765485763484a4358774d",
                "userId": "59b23449b20b7c1838eee1a3",
                "tickets": [
                    {
                        "keyId": "6d7435625564594f4a563554796c6a77",
                        "iv": "d2e2de0f9387c5d9b16424e8ac66a3c1"
                    }
                ]
            },
            {
                "id": "3451483977564d755278397939593633",
                "userId": "59b23449b20b7c1838eee1a3",
                "tickets": [
                    {
                        "keyId": "6d7435625564594f4a563554796c6a77",
                        "iv": "582ff50ac3d337c62eb53094470e3161"
                    }
                ]
            },
            {
                "id": "7059684f4e42754d55456e726b35664e",
                "userId": "59b23449b20b7c1838eee1a3",
                "tickets": [
                    {
                        "keyId": "6d7435625564594f4a563554796c6a77",
                        "iv": "b110ee5cb5da8941cc8ad6e1c3fe501c"
                    }
                ]
            }
        ]
    }
]

使用keyId=6d7435625564594f4a563554796c6a77删除所有故障单后,预期数据应如下所示:

[
    {
        "_id": "59fe54098448d822f89a7e62",
        "ownerId": "59b23449b20b7c1838eee1a3",
        "name": "Home",
        "keys": [
            {
                "id": "6d7435625564594f4a563554796c6a77",
                "name": "Front Door"
            }
        ],
        "grants": [
            {
                "id": "307658775634774a556b677650347072",
                "userId": "59b23449b20b7c1838eee1a3",
                "tickets": []
            },
            {
                "id": "37703369365765485763484a4358774d",
                "userId": "59b23449b20b7c1838eee1a3",
                "tickets": []
            },
            {
                "id": "3451483977564d755278397939593633",
                "userId": "59b23449b20b7c1838eee1a3",
                "tickets": []
            },
            {
                "id": "7059684f4e42754d55456e726b35664e",
                "userId": "59b23449b20b7c1838eee1a3",
                "tickets": []
            }
        ]
    }
]

此代码一次删除所有授权:

db.places.update({}, {
    $pull: {
        "grants": {
            "tickets": {
                $elemMatch: { "keyId": keyID }
            }
        }
    }
}, { multi: true });

这只取出第一张票,然后用" $ pullAll"什么都不做:

db.places.findAndModify(
    {
        ownerId: ownerID, "grants.tickets.keyId": keyID
    },
    [ ],
    { $pull: { "grants.$.tickets": { keyId: keyID } } },
    { multi: true },
    next
);

这引发了一个错误说:无法使用该部分(grants.tickets。$ * .keyId的授权)来遍历该元素

db.places.update({ "grants.tickets.keyId": keyID }, {
    $pull: {
        "grants.tickets.$*.keyId": keyID
    }
}, { multi: true });

0 个答案:

没有答案