[英]Data not getting fetched using aggregation query in nodejs
我尝试了下面的代码,但只检查并显示了第一个匹配项,其他显示为 object,为什么我无法在控制台中看到它。 我有 3 个 collections 学生学科老师,也为相同的模式。 尝试聚合
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
的更多信息,请参阅此处。
希望这会有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.