一天之后,
我解决了我的问题
. 有人可以判断聚合是否有效(如果有问题,给我一个建议)?
allPolls = await Poll.aggregate([
{
$match: {
"toUser": ObjectID(user.id),
$or: [
{status: "opened"},
{status: "closed"},
{status: "hidden"},
{status: "waitingResponse"}
]
}
},
{
$lookup: {
"from": "replies",
"localField": "replies",
"foreignField": "_id",
"as": "replies"
}
},
{
$lookup: {
"from": "replies",
"localField": "replySelected",
"foreignField": "_id",
"as": "selectedReply"
}
},
{
$lookup: {
"from": "users",
"localField": "author",
"foreignField": "_id",
"as": "creator"
}
},
{
$project: {
status: 1,
toUser: 1,
_id: 1,
createdAt: 1,
question: 1,
replies: 1,
anonymous: 1,
"replySelected._id": 1,
"author.username": {
$cond: {
if: "$anonymous", then: null, else : "$creator.username"
}
},
"author._id": {
$cond: {
if: "$anonymous", then: null, else : "$creator._id"
}
},
}
},
{
$skip: 0
},
{
$limit: 2
}
]);
(我尝试了固定的跳过/限制)