如何在mongodb聚合中使用$ lookup和$ in

时间:2019-05-10 08:22:16

标签: mongodb mongodb-query aggregation-framework

  

学院

{
    "_id" : ObjectId("5cd42b5c65b41027845938ae"),
    "clgID" : "100",
    "name" : "Vivekananda"
},

{
    "_id" : ObjectId("5cd42b5c65b41027845938ad"),
    "clgID" : "200",
    "name" : "National"
}

点:1 =>将所有clgID从Colleges集合中获取。

  

主题:

{
    "_id" : ObjectId("5cd42c2465b41027845938b0"),
    "name" : "Hindi",
    "members" : {
        "student" : [
            "123"
        ]
    },
    "college" : {
        "collegeID" : "100"
    }
},

{
    "_id" : ObjectId("5cd42c2465b41027845938af"),
    "name" : "English",
    "members" : {
        "student" : [
            "456",
            "789"
        ]
    },
    "college" : {
        "collegeID" : "100"
    }
}

点:2 => Subjects集合我们映射到{{1}下的clgID,主题集合我们需要基于college.collegeID来获取members.student的值

  

学院产品

clgID

点:3 => { "_id" : "123", "StudentProdcutID" : "123", "StudentID" : "FF80", "CID" : "Facebook" }, { "_id" : "456", "StudentProdcutID" : "456", "StudentID" : "FF81", "CID" : "Facebook" }, { "_id" : "789", "StudentProdcutID" : "789", "StudentID" : "FF82", "CID" : "Facebook" } 集合,我们在CollegeProducts下映射了members.student的值,CollegeProducts集合,我们需要获取StudentProdcutID中的值。 StudentID集合,我们需要检查条件CollegeProducts应该为CID,并基于Facebook来获取StudentID的值。

  

UserDetails

members.student

点:3 => { "name" : "A", "StudentID" : "FF80" }, { "name" : "B", "StudentID" : "FF81" }, { "name" : "C", "StudentID" : "FF82" } 集合,我们在UserDetails下映射了StudentID值,UserDetails集合我们需要取StudentID的值。

  

预期输出:

name
  

我的代码

{
"collegeName" : "National",
"StudentName" : "A"
},
{
"collegeName" : "National",
"StudentName" : "B"
},
{
"collegeName" : "National",
"StudentName" : "C"
}

我没有达到预期的输出,请帮助我。我正在使用 mongodb version3.4

2 个答案:

答案 0 :(得分:1)

如果您希望每个输出成为一个用户,不要打扰分组,您要做的只是两倍的工作。

将查询更改为此:

    { 
        "$match" : {
            "clgID" : {
                "$in" : [
                    "100", 
                    "200"
                ]
            }
        }
    }, 
    { 
        "$lookup" : {
            "from" : "Subjects", 
            "localField" : "clgID", 
            "foreignField" : "college.collegeID", 
            "as" : "clg"
        }
    }, 
    { 
        "$unwind" : {
            "path" : "$clg", 
            "preserveNullAndEmptyArrays" : true
        }
    }, 
    { 
        "$unwind" : {
            "path" : "$clg.members.student", 
            "preserveNullAndEmptyArrays" : true
        }
    }, 
    { 
        "$project" : {
            "collegeName" : "$name", 
            "student" : "$clg.members.student"
        }
    }
], 

现在第二步展开时,每个对象都包含大学名称和-ONE-学生,因此我们现在要做的就是以所需的形式进行项目设计。

编辑:根据要求进行完整查询

    { 
        "$match" : {
            "clgID" : {
                "$in" : [
                    "100", 
                    "200"
                ]
            }
        }
    }, 
    { 
        "$lookup" : {
            "from" : "Subjects", 
            "localField" : "clgID", 
            "foreignField" : "college.collegeID", 
            "as" : "clg"
        }
    }, 
    { 
        "$unwind" : {
            "path" : "$clg", 
            "preserveNullAndEmptyArrays" : true
        }
    }, 
    { 
        "$unwind" : {
            "path" : "$clg.members.student", 
            "preserveNullAndEmptyArrays" : true
        }
    }, 
    { 
        "$lookup" : {
            "from" : "CollegeProducts", 
            "localField" : "clg.members.student", 
            "foreignField" : "StudentProdcutID", 
            "as" : "clgproduct"
        }
    }, 
    {   // can skip this unwind if theres always only one match.
        "$unwind" : {
            "path" : "$clgproduct", 
            "preserveNullAndEmptyArrays" : true
        }
    }, 
    { 
        "$match" : {
            "clgproduct.CID" : "Facebook"
        }
    }, 
    { 
        "$lookup" : {
            "from" : "UserDetails", 
            "localField" : "clgproduct.StudentID", 
            "foreignField" : "StudentID", 
            "as" : "student"
        }
    }, 
    {   // can skip this unwind if theres always only one user matched.
        "$unwind" : {
            "path" : "$student", 
            "preserveNullAndEmptyArrays" : true
        }
    }, 
    { 
        "$project" : {
            "collegeName" : "$name", 
            "student" : "$student.name"
        }
    }
], 

答案 1 :(得分:1)

您可以使用以下汇总

db.Colleges.aggregate([
  { "$match": { "clgID": { "$in": ["100", "200"] }}},
  { "$lookup": {
    "from": "Subjects",
    "localField": "clgID",
    "foreignField": "college.collegeID",
    "as": "clg"
  }},
  { "$unwind": { "path": "$clg", "preserveNullAndEmptyArrays": true }},
  { "$group": {
    "_id": { "clgId": "$clg.college.collegeID", "_id": "$_id" },
    "groupDetails": { "$push": "$clg.members.student" },
    "clgName": { "$first": "$name" }
  }},
  { "$project": {
    "_id": "$_id._id",
    "clgName": 1,
    "groupDetails": {
      "$reduce": {
        "input": "$groupDetails",
        "initialValue": [],
        "in": { "$concatArrays": ["$$this", "$$value"] }
      }
    }
  }},
  { "$lookup": {
    "from": "CollegeProduct",
    "localField": "groupDetails",
    "foreignField": "StudentProdcutID",
    "as": "CollegeProduct"
  }},
  { "$unwind": "$CollegeProduct" },
  { "$lookup": {
    "from": "UserDetails",
    "localField": "CollegeProduct.StudentID",
    "foreignField": "StudentID",
    "as": "Student"
  }},
  { "$unwind": "$Student" },
  { "$project": { "collegeName": "clgName", "StudentName": "$Student.name" }}
])

MongoPlayground

Output

[
  {
    "StudentName": "A",
    "_id": ObjectId("5cd42b5c65b41027845938ae"),
    "collegeName": "clgName"
  },
  {
    "StudentName": "B",
    "_id": ObjectId("5cd42b5c65b41027845938ae"),
    "collegeName": "clgName"
  },
  {
    "StudentName": "C",
    "_id": ObjectId("5cd42b5c65b41027845938ae"),
    "collegeName": "clgName"
  }
]