如何通过与文档中其他字段相关的嵌套数组中的id格式化文档

时间:2019-03-15 03:47:04

标签: mongodb mongodb-query embedded-documents nested-documents

给出这样的集合:

{
    "_id": ObjectId('5c8a0ec45e9dc27ddabe25ff'),
    "disks": [
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fca'),
            "disk_val": "892086fdd23d43db8e2b2494217fd869"
        },
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fcc'),
            "disk_val": "70be62e594ec6b1e1c25202d5c3dd56e",
            "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
        },
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fce'),
            "disk_val": "ca0379dc852d67cbf6de7a70c56ff700",
            "raid_logic_id": ObjectId('5c8a0ec35e9dc27ddabd4fcd'),
            "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
        }
    ],
    "raid_cards": [
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb'),
            "raid_card_val": "a9345e661d970dd784b6349c14c17f36",
            "disks": [
                ObjectId('5c8a0ec35e9dc27ddabd4fcc')
            ],
            "raid_logics": [
                ObjectId('5c8a0ec35e9dc27ddabd4fcd')
            ]
        }
    ],
    "raid_logics": [
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fcd'),
            "raid_logic_val": "e6232a8a72166d8916c75ea9a4e16104",
            "disks": [
                ObjectId('5c8a0ec35e9dc27ddabd4fce')
            ],
            "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
        }
    ]
}

如何用相关的嵌套文档替换嵌套id字段?我希望我的输出是这样的:

{
    "_id": ObjectId('5c8a0ec45e9dc27ddabe25ff'),
    "disks": [
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fca'),
            "disk_val": "892086fdd23d43db8e2b2494217fd869"
        },
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fcc'),
            "disk_val": "70be62e594ec6b1e1c25202d5c3dd56e",
            "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
        },
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fce'),
            "disk_val": "ca0379dc852d67cbf6de7a70c56ff700",
            "raid_logic_id": ObjectId('5c8a0ec35e9dc27ddabd4fcd'),
            "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
        }
    ],
    "raid_cards": [
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb'),
            "raid_card_val": "a9345e661d970dd784b6349c14c17f36",
            "disks": [
                {
                    "_id": ObjectId('5c8a0ec35e9dc27ddabd4fcc'),
                    "disk_val": "70be62e594ec6b1e1c25202d5c3dd56e",
                    "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
                }
            ],
            "raid_logics": [
                {
                    "_id": ObjectId('5c8a0ec35e9dc27ddabd4fcd'),
                    "raid_logic_val": "e6232a8a72166d8916c75ea9a4e16104",
                    "disks": [
                        {
                            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fce'),
                            "disk_val": "ca0379dc852d67cbf6de7a70c56ff700",
                            "raid_logic_id": ObjectId('5c8a0ec35e9dc27ddabd4fcd'),
                            "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
                        }
                    ],
                    "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
                }
            ]
        }
    ],
    "raid_logics": [
        {
            "_id": ObjectId('5c8a0ec35e9dc27ddabd4fcd'),
            "raid_logic_val": "e6232a8a72166d8916c75ea9a4e16104",
            "disks": [
                {
                    "_id": ObjectId('5c8a0ec35e9dc27ddabd4fce'),
                    "disk_val": "ca0379dc852d67cbf6de7a70c56ff700",
                    "raid_logic_id": ObjectId('5c8a0ec35e9dc27ddabd4fcd'),
                    "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
                }
            ],
            "raid_card_id": ObjectId('5c8a0ec35e9dc27ddabd4fcb')
        }
    ]
}

我已经尝试过了,但是没有用,看来$raid_logics.disks不是数组:

db.col.aggregate(
    [
        {
            $limit: 1
        },
        {
            $project: {
                "disks": 1,
                "raid_logics.disks": {
                    $filter: {
                        input: "$disks",
                        as: "disks",
                        cond: {
                            $in: [
                                "$$disks._id",
                                "$raid_logics.disks"
                            ]
                        }
                     }
                }
            }
        }
    ]
)

0 个答案:

没有答案