如何在mongodb聚合查询中使用getSiblingDB

时间:2019-05-12 10:31:31

标签: javascript mongodb mongodb-query aggregate

  1. 从“大学”合集中提取所有clgID
  2. Subjects集合,我们在clgID下映射了college.collegeID,Subjects集合,我们需要基于members.student来获取clgID的值。
  3. CollegeProducts集合,我们在members.student下映射了StudentProdcutID值,CollegeProducts集合,我们需要获取StudentID中的值。 CollegeProducts集合,我们需要检查条件CID应该为Facebook,并基于StudentID来获取members.student的值。
  4. UserDetails集合,我们在StudentID下映射了StudentID值,UserDetails集合我们需要采用name的值。

我的数据库名称:testing1


  

学院

{
    "_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

点:4 => { "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"
}

此代码可以正常工作,现在只有我的问题会开始

我还有一个名为db.Colleges.aggregate([ { "$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" } }, { "$unwind" : { "path" : "$clgproduct", "preserveNullAndEmptyArrays" : true } },// can skip this unwind if theres always only one match. { "$match" : { "clgproduct.CID" : "Facebook" } }, { "$lookup" : { "from" : "UserDetails", "localField" : "clgproduct.StudentID", "foreignField" : "StudentID", "as" : "student" } }, { "$unwind" : { "path" : "$student", "preserveNullAndEmptyArrays" : true } },// can skip this unwind if theres always only one user matched. { "$project" : { "collegeName" : "$name", "student" : "$student.name" } } ]); 的数据库,集合名称为testing2

  

UsersLog

UsersLog

在此集合中,我们将明智地存储用户(CollegeProducts-> StudentProdcutID) loginTime logoutTime

我的问题是,谁都登录的次数超过{ "_id" : "5cd7f08ddba6e6300400387a", "StudentPID" : "456", "loginTime" : "2019-05-12 10:30:00", "logoutTime" : "2019-05-12 15:30:00" }, { "_id" : "5cd7f08ddba6e6300400387a", "StudentPID" : "456", "loginTime" : "2019-05-12 16:00:00", "logoutTime" : "2019-05-12 23:00:00" }, { "_id" : "5cd7f08ddba6e6300400387a", "StudentPID" : "123", "loginTime" : "2019-05-12 16:00:00", "logoutTime" : "2019-05-12 23:00:00" } ,那么只有我必须显示用户名。

10 hrs集合中,我们将StudentPID的值映射到StudentPID下,

主要事物UsersLogStudentPID数据库之下,而testing1UsersLog数据库之下

  

现在我期望的答案

testing2

因为{ "collegeName" : "National", "StudentName" : "B" } 仅登录了两次,并计算了登录时间和注销时间,StudentProdcutID = 456

更新的代码部分

10 hrs

0 个答案:

没有答案