我尝试了下面的代码,但只有第一个匹配被检查并显示,其他显示为对象,这就是为什么我无法在控制台中看到它。我有三位收集学生科目的老师,也为之做了图式。尝试聚合
Student.aggregate([
{
$match: { name: 'abcd'}
},
{
$lookup:
{
from:'teachers',
pipeline: [{ $match: { name: 'pqrs' } },],
as: "teacherLookup"
}
},
{
$lookup:
{
from:'subjects',
pipeline: [{ $match: { name: 'computer' } }],
as: "subjectLookup"
}
}
]).then(function (res) {
console.log(res);
res.forEach(function(students){
let id = students._id;
console.log(id+ ' got id ')
}
output
student
name:'abcd' -- its fetched and other two not displaying values only shows object
teacherLookup: [ [Object] ]
subjectLookup: [ [Object] ]
您在那里只是为了投影一些必须使用$project
阶段的东西。
我在这里添加查询:
Student.aggregate([
{
$match: { name: 'abcd'}
},
{
$lookup:{
from:'teachers',
pipeline: [
{
$match: { name: 'pqrs' }
},
{
$project:{
"_id":1
}
}
],
as: "teacherLookup"
}
},
{
$lookup:
{
from:'subjects',
pipeline: [
{
$match: { name: 'computer' }
},
{
$project:{
"_id":1
}
}
],
as: "subjectLookup"
}
}
])
有关$project
的更多信息,请参阅此处。
希望这将有所帮助:(