clgID
。Subjects
集合,我们在clgID
下映射了college.collegeID
,Subjects集合,我们需要基于members.student
来获取clgID
的值。CollegeProducts
集合,我们在members.student
下映射了StudentProdcutID
值,CollegeProducts集合,我们需要获取StudentID
中的值。 CollegeProducts
集合,我们需要检查条件CID
应该为Facebook
,并基于StudentID
来获取members.student
的值。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下,
主要事物UsersLog
在StudentPID
数据库之下,而testing1
在UsersLog
数据库之下
现在我期望的答案
testing2
因为{
"collegeName" : "National",
"StudentName" : "B"
}
仅登录了两次,并计算了登录时间和注销时间,StudentProdcutID = 456
更新的代码部分
10 hrs