多重聚合查询MongoDB

时间:2018-08-06 17:23:52

标签: javascript mongodb mongodb-query aggregation-framework

我有以下情况:

  • 集合departments

    { "_id" : 0, "name" : "Dep_A_1", "description" : "Department A1" }
    { "_id" : 1, "name" : "Dep_B_1", "description" : "Department B1" }
    { "_id" : 2, "name" : "Dep_C_1", "description" : "Department C1" }
    
  • 集合skills

    { 
        "_id" : 0, 
        "name" : "Creativity", 
        "description" : "description", 
        "type" : "soft", 
        "categories" : [ 0, 2 ] 
    }
    
  • 集合roles

    { 
        "_id" : 0,
        "name" : "Software Developer Automation and Control",
        "description" : "Automation and Control expert",
        "departments" : [ 0, 2 ],
        "skills" : [ { "_id" : 18, "weight" : 30 } ]
    }
    

我需要这样的结果:

{
    "_id" : 0, 
    "name" : "Software Developer Automation and Control", 
    "description" : "Automation and Control expert",
    "departments" : [ 
        { 
            "_id" : 0, 
            "name" : "Dep_A_1",
            "description" : "Department A1" 
        }, 
        { 
            "_id" : 2, 
            "name" : "Dep_C_1", 
            "description" : "Department C1" 
        }
    ], 
    "skills" : [ 
        {
            "_id" : 0, 
            "name" : "Creativity", 
            "description" : "description", 
            "type" : "soft", 
            "weight" : 30
        }
    ]
}

我需要用role.departmentsrole.skills集合中的相应对象替换departmentsroles数组。有没有办法查询Mongo并得到这样的结果?

无论如何,我正在使用Mongo 3.6和Pymongo。 谢谢。

2 个答案:

答案 0 :(得分:1)

只需两次使用$lookup

db.getCollection('roles').aggregate([
    {$lookup: {
        from: 'departments',
        localField: 'departments',
        foreignField: '_id',
        as: 'departments'
    }},
    {$lookup: {
        from: 'skills',
        localField: 'skills._id',
        foreignField: '_id',
        as: 'skills'
    }}
])

编辑:现在roles.skills.weight已正确保存:

db.getCollection('roles').aggregate([
    {$unwind: '$skills'},
    {$lookup: {
        from: 'skills',
        localField: 'skills._id',
        foreignField: '_id',
        as: 'skillsInfo'
    }},
    {$unwind: '$skillsInfo'},
    {$addFields: {skills: {$mergeObjects: ['$skills', '$skillsInfo']}}},
    {$project: {skillsInfo: 0}},
    {$group: {
        _id: '$_id',
        name: {$first: '$name'},
        description: {$first: '$description'},
        departments: {$first: '$departments'},
        skills: {$push: '$skills'}
    }},
    {$lookup: {
        from: 'departments',
        localField: 'departments',
        foreignField: '_id',
        as: 'departments'
    }}
])

答案 1 :(得分:1)

为了避免进行昂贵的$unwind$group阶段,您可以这样做:

db.roles.aggregate([{
    $lookup: {
        from: 'departments',
        localField: 'departments',
        foreignField: '_id',
        as: 'departments'
    }
}, {
    $lookup: {
        from: 'skills',
        let: {
            "skills": "$skills" // keep a reference to the "skills" field of the current document and make it accessible via "$$skills"
        },
        pipeline: [{
            $match: {
                $expr: {
                    $in: [ "$_id", "$$skills._id" ] // this just does the "joining"
                }
            }
        }, {
            $addFields: { // add a new field
                "weight": { // called "weight"
                    $arrayElemAt: [ // which shall be set to the correct "weight"
                        "$$skills.weight", // that can be found in the "weight" field of the "skills" array
                        { $indexOfArray: [ "$$skills._id", "$_id" ] } // at the position that has the matching "_id" value
                    ]
                }
            }
        }],
        as: 'skills'
    }
}])