将整个原始文档放在新的属性名称下

时间:2017-07-26 17:15:32

标签: node.js mongodb mongoose mongodb-query aggregation-framework

我创建了一个聚合管道,它执行以下操作:

  1. 转到自定义集合并按_id
  2. 查找
  3. 转到默认收藏集并按ID查找,将其附加为默认
  4. 转到样式集合并按ID查找,将其附加为样式
  5. 代码:

    Custom.aggregate([{
            $match: {
                '_id': mongoose.Types.ObjectId(req.params.id)
            }
        },
        {
            $lookup: {
                from: 'styles',
                localField: 'styleId',
                foreignField: 'id',
                as: 'style'
            }
        },
        {
            $unwind: '$style'
        },
        {
            $lookup: {
                from: 'default',
                localField: 'styleId',
                foreignField: 'id',
                as: 'default'
            }
        },
        {
            $unwind: '$default'
        },
    ]).then(docs => res.json(docs))
    }
    

    这是当前的输出:

    {
        "_id": "597774cc09064e2e4e9fedb7",
        "updatedAt": "2017-07-25T16:41:48.823Z",
        "createdAt": "2017-07-25T16:41:48.823Z",
        "styleId": "401648805",
        "colorId": null,
        "selectedOption": null,
        "deselectedOption": null,
        "currentOptions": [],
        "requiredItems": [],
        "includedItems": [],
        "furtherRemovals": [],
        "furtherAdditions": [],
        "excludedItems": [],
        "name": [],
        "__v": 0,
        "style": {
            "_id": "5902276effabb8bd2926be71",
            "id": "401648805",
            "name": "Luxury 4dr Sedan (2.0L 4cyl Turbo 8A)",
            "createdAt": "2017-07-26T15:28:36.778Z",
            "updatedAt": "2017-07-26T15:49:14.366Z"
        },
        "default": {
            "_id": "5978bfef32d246877e9cb363",
            "updatedAt": "2017-07-26T16:14:39.779Z",
            "createdAt": "2017-07-26T16:14:39.779Z",
            "id": "401648805",
            "requiredItems": [],
            "includedItems": [],
            "furtherRemovals": [],
            "furtherAdditions": [],
            "excludedItems": [],
            "__v": 0
        }
    }
    

    我正在尝试将自定义对象包装在“自定义”命名对象中,它更适合我们的数据结构:

    {
        "custom": {
            "_id": "597774cc09064e2e4e9fedb7",
            "updatedAt": "2017-07-25T16:41:48.823Z",
            "createdAt": "2017-07-25T16:41:48.823Z",
            "styleId": "401648805",
            "colorId": null,
            "selectedOption": null,
            "deselectedOption": null,
            "currentOptions": [],
            "requiredItems": [],
            "includedItems": [],
            "furtherRemovals": [],
            "furtherAdditions": [],
            "excludedItems": [],
            "name": [],
            "__v": 0,
        },
        "style": {
            "_id": "5902276effabb8bd2926be71",
            "id": "401648805",
            "name": "Luxury 4dr Sedan (2.0L 4cyl Turbo 8A)",
            "createdAt": "2017-07-26T15:28:36.778Z",
            "updatedAt": "2017-07-26T15:49:14.366Z"
        },
        "default": {
            "_id": "5978bfef32d246877e9cb363",
            "updatedAt": "2017-07-26T16:14:39.779Z",
            "createdAt": "2017-07-26T16:14:39.779Z",
            "id": "401648805",
            "requiredItems": [],
            "includedItems": [],
            "furtherRemovals": [],
            "furtherAdditions": [],
            "excludedItems": [],
            "__v": 0
        }
    }
    

    我认为它与聚合管道中的$group有关,但我不知道如何构建它。任何帮助将不胜感激 - 对MonogoDB / Mongoose来说相对较新。

2 个答案:

答案 0 :(得分:2)

$project之后,我个人$match只需$$ROOT Custom.aggregate([ { $match: { '_id': mongoose.Types.ObjectId(req.params.id) }}, { $project: { _id: 0, custom: '$$ROOT' } }, { $lookup: { from: 'styles', localField: 'custom.styleId', foreignField: 'id', as: 'style' }}, { $unwind: '$style' }, { $lookup: { from: 'default', localField: 'custom.styleId', foreignField: 'id', as: 'default' }}, { $unwind: '$default' } ]).then(docs => res.json(docs)) 新名称“就在管道的开头”:

multiprocessing

$lookup引入以来,它是所有版本中代码最少且兼容的。

答案 1 :(得分:1)

可能有更好的方法,但在管道末端使用$replaceRoot可以获得所需的结构(未经测试):

Custom.aggregate([{
        $match: {
            '_id': mongoose.Types.ObjectId(req.params.id)
        }
    },
    {
        $lookup: {
            from: 'styles',
            localField: 'styleId',
            foreignField: 'id',
            as: 'style'
        }
    },
    {
        $unwind: '$style'
    },
    {
        $lookup: {
            from: 'default',
            localField: 'styleId',
            foreignField: 'id',
            as: 'default'
        }
    },
    {
        $unwind: '$default'
    },

    {
        $replaceRoot: {
            newRoot: {
                "custom": {
                    "_id": "$_id",
                    "updatedAt": "$updatedAt",
                    "createdAt": "$createdAt",
                    "styleId": "$styleId",
                    "colorId": "$colorId",
                    "selectedOption": "$selectedOption",
                    "deselectedOption": "$deselectedOption",
                    "currentOptions": "$currentOptions",
                    "requiredItems": "$requiredItems",
                    "includedItems": "$includedItems",
                    "furtherRemovals": "$furtherRemovals",
                    "furtherAdditions": "$furtherAdditions",
                    "excludedItems": "$excludedItems",
                    "name": "$name",
                    "__v": "$__v",
                },
                "style": "$style",
                "default": "$default"

            }
        }
    }

]).then(docs => res.json(docs))
}

要使用此功能,您的mongo版本应为3.4或更高。