【发布时间】:2021-01-21 23:01:20
【问题描述】:
我有两个名为 School 和 Students
的集合学校收藏
{
_id: ObjectId("60008e81d186a82fdc4ff2b7"),
name: String,
branch: String,
class: [{
"active" : true,
"_id" : ObjectId("6001e6871d985e477b61b43f"),
"name" : "I",
"order" : 1
},
{
"active" : true,
"_id" : ObjectId("6001e68f1d985e477b61b444"),
"name" : "II",
"order" : 2
}]
}
学生收藏
{
_id: ObjectId("6002def815eccd53a596f830"),
schoolId: ObjectId("60008e81d186a82fdc4ff2b7"),
sessionId: ObjectId("60008e81d186a82fdc4ff2b9"),
class: ObjectId("6001e6871d985e477b61b43f"),
}
我想在单个查询中获取Student Collection的数据。
我的班级id 存储在学生集合中,针对该id 的数据存储在学校集合中的类键下,这是一个对象数组。
您能帮我在学生集合中获取具有此 ID 的类对象吗?
我想要的输出:
data: {
_id: ObjectId("6002def815eccd53a596f830"),
schoolId: ObjectId("60008e81d186a82fdc4ff2b7"),
sessionId: ObjectId("60008e81d186a82fdc4ff2b9"),
class: ObjectId("6001e6871d985e477b61b43f"),
classData: [{
"active" : true,
"_id" : ObjectId("6001e6871d985e477b61b43f"),
"name" : "I",
"order" : 1
}]
}
所以我尝试了这个,但没有成功:
const students = await this.studentModel.aggregate([
{
$lookup: {
from: 'School',
let: { classId: '$class' },
pipeline: [
{
$match: {
$expr: { $eq: ['$$classId', '$class._id'] },
},
},
],
as: 'classData',
},
},
]);
【问题讨论】:
-
在您的查询中尝试使用
$in而不是$eq。 -
@turivishal 我试过没用
-
确保集合名称
School或Schools在数据库中。 -
是的,他们都是学校和学生,问题在于查询。
-
当你使用
$in而不是$eq时,查询看起来不错,在本地机器上测试过。
标签: mongodb mongodb-query aggregation-framework mongoose-schema mongoose-populate