[英]mongoose aggregation not returning expected result
I am currently learning aggregation and some related methods, after following mongoose and mongodb docs i tried it. 在尝试了mongoose和mongodb文档之后,我目前正在学习聚合和一些相关方法。 but i am having problem.
但我有问题。
Follow.aggregate([
{ $match: { user: mongoose.Types.ObjectId(userId) } },
{ $unwind: '$followers' },
{
$lookup: {
from: 'accounts',
localField: 'followers',
foreignField: '_id',
as: 'followers'
}
},
{ $project: { name: 1, photo: 1 } },
]).exec((err, followers) => {
if (err) throw err;
console.log(followers);
res.send(followers);
});
I want to get the followers of that userID and select the followers names and photo, but i am only getting the objectid of the matched document 我想获取该用户ID的关注者,并选择关注者名称和照片,但我仅获取匹配文档的objectid
[ { _id: 5bfe2c529419a560fb3e92eb } ]
expected output 预期产量
[ { _id: 5bfe2c529419a560fb3e92eb , name: 'john doe", photo: 'cat.jpg'} ]
Follow Model 跟随模型
const FollowSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'Account',
required: true,
},
following: [{
type: Schema.Types.ObjectId,
ref: 'Account',
required: true,
}],
followers: [{
type: Schema.Types.ObjectId,
ref: 'Account',
required: true,
}],
});
When the $project
stage is wrong you would get that result. 当
$project
阶段错误时,您将得到该结果。 Try removing it first and see the output. 尝试先将其删除,然后查看输出。 To me it seems you should have something like this there:
对我来说,您似乎应该在这里有以下内容:
{ $project: { "followers.name": 1, "followers.photo": 1 } },
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.