问题描述
我有2个模型,一个是小组,另一个是学生.组看起来像这样
I have 2 models, one is Group and another is Students. Group looks like this
{
"_id" : ObjectId("5c8d28ef7e0e542854b7b904"),
"name" : "Homeroom ",
"year" : 2019,
"schoolID" : ObjectId("5c1a735fc98da061141475a1"),
"teachers" : [
{
"_id" : "5c1a7677c98da061141475aa",
"firstName" : "Ayam"
},
{
"_id" : "5c1a7677c98da061141475a9",
"firstName" : "Itik"
}
],
"addedOn" : ISODate("2019-03-16T16:48:47.372Z"),
"lastModified" : ISODate("2019-03-16T16:48:47.372Z"),
"__v" : 0,
"status" : 1,
"students" : [
"5c1a79f7c98da061141475b7",
"5c3bfea37774fb0b55000cb5",
"5c1a7c69c98da061141475bb",
"5c3bfea37774fb0b55000cb4",
"5c1a7d32c98da061141475be",
"5c3bfea37774fb0b55000cb7"
]
}
上面的字段students
存储学生模型中学生的_id(以String格式).
Where the field students
above stores the _id (in String format) of students inside Students model.
现在我正在尝试使用聚合进行查找,并且我想到了这样的东西:
Now I'm trying to do a lookup with aggregate, and I comes up with something like this:
Group.aggregate([
{ $match: { _id: mongoose.mongo.ObjectId(groupID) } },
{ $lookup: {
from: "Students", localField: "students", foreignField: "_id", as: "studentList"
} },
{ $unwind: "$studentList" },
{ $replaceRoot: { newRoot: "$students" } }
], function(err, result){
if (err){
console.log("imin err 102: " )
console.log(err)
}else{
console.log("imini 105 result")
console.log(result);
}
});
现在,我知道上面的代码不会产生任何结果,因为模型组中的students
存储为字符串,而模型Student中的_id是ObjectId. Mongodb现在有了$toObjectId
,但是由于我的模型拥有一个String数组,所以我不知道如何正确实现$toObjectId
.
Now I understand that the code above won't yield any result, since students
inside Model Group is stored as String, while the _id inside model Student is an ObjectId. Mongodb now have $toObjectId
but since my Model holds an array of String, I don't know how to implement $toObjectId
correctly.
这里是学生的示例文件
{
"_id" : ObjectId("5c1a79f7c98da061141475b7"),
"firstName" : "Ibrahim",
"kelasID" : ObjectId("5c429f9906f2a805bc6cd494"),
"lastName" : "Ali",
"schoolID" : ObjectId("5c1a735fc98da061141475a1"),
"year" : 2018,
"__v" : 0,
"addedOn" : ISODate("2018-12-25T04:27:47.909Z"),
"checkIn" : false,
"checkInStatus" : 1,
"contactNo1" : "012225656",
"father" : "Ali",
"fatherID" : "8852245",
"idType" : 0,
"lastModified" : ISODate("2018-12-25T04:27:47.909Z"),
"mother" : "",
"motherID" : ""
}
{
"_id" : ObjectId("5c3bfea37774fb0b55000cb5"),
"idType" : 0,
"checkIn" : false,
"checkInStatus" : 1,
"year" : 2019,
"schoolID" : ObjectId("5c1a735fc98da061141475a1"),
"kelasID" : ObjectId("5c1a7534c98da061141475a3"),
"firstName" : "Umar",
"lastName" : "Bin Al-Khattab",
"contactNo1" : "601222",
"status" : 1,
"addedOn" : ISODate("2019-01-14T03:14:43.597Z"),
"lastModified" : ISODate("2019-01-14T03:14:43.597Z"),
"__v" : 0
}
{
"_id" : ObjectId("5c1a7c69c98da061141475bb"),
"idType" : 0,
"checkIn" : false,
"checkInStatus" : 1,
"year" : 2018,
"schoolID" : ObjectId("5c1a735fc98da061141475a1"),
"kelasID" : ObjectId("5c1a7540c98da061141475a5"),
"firstName" : "Abdul Rahman",
"lastName" : "Affan",
"father" : "Affan",
"fatherID" : "54321",
"contactNo1" : "602288",
"status" : 1,
"addedOn" : ISODate("2018-12-25T04:30:16.130Z"),
"lastModified" : ISODate("2018-12-25T04:30:16.130Z"),
"__v" : 0
}
推荐答案
您必须 students
字段上的"> $map
,将String
id转换为ObjectId
You have to $map
over the students
field to convert the String
ids to ObjectId
Group.aggregate(
[
{ "$match": { "_id": mongoose.mongo.ObjectId(groupID) } },
{ "$addFields": {
"students": {
"$map": {
"input": "$students",
"in": { "$toObjectId": "$$this" }
}
}
}},
{
"$lookup": {
"from": "Students",
"localField": "students",
"foreignField": "_id",
"as": "studentList"
}
},
{ "$unwind": "$studentList" },
{ "$replaceRoot": { "newRoot": "$students" } }
],
function(err, result) {
if (err) {
console.log("imin err 102: ");
console.log(err);
} else {
console.log("imini 105 result");
console.log(result);
}
}
)
这篇关于toObjectId,用于将字符串的字段与查找和聚合一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!