【发布时间】:2016-02-28 10:25:58
【问题描述】:
我有一个看起来像这样的猫鼬模式:
var AnswerSchema = new Schema({
author: {type: Schema.Types.ObjectId, ref: 'User'},
likes: [{type: Schema.Types.ObjectId, ref: 'User'}],
date: {type: Date, default: Date.now},
text: String,
....
});
截至目前,我通过以下方式查询此集合:
Answer.find({
author: profileId,
date: {$lt: fromDate}
}).sort({date: -1})
.limit(25)
.populate('question.sender', 'username username_display name')
.exec(function (err, answers) {
*code here*
});
但是由于聚合管道,我需要在此查询中添加一个计算字段,以了解访问者是否已经喜欢作者的答案(请参阅this question)。它看起来像这样:
Answer.aggregate()
.project({
"author": 1,
"matched": {
"$eq": [
{
"$size": {
"$ifNull": [
{ "$setIntersection": [ "$likes", [userId] ] },
[]
]
}
},
1
]
}
})
.exec(function (err, docs){
console.log(docs);
})
问题是:我只需要对由第一个查询过滤的文档运行此投影。换句话说,我需要将 find/limit/sort 与项目结合起来。
我怎么能这样做?我尝试在第一个查询之后(在 .exec() 之前)链接 .project(),但它不起作用。也许我需要通过 mongodb 的聚合链完成我在第一个查询中所做的所有事情?
编辑: 这是一个数据集的例子来试试这个:
{
"_id" : ObjectId("56334295e45552c018fc475d"),
"author" : ObjectId("561f9c319cdd94401ae160ef"),
"likeCount" : 1,
"likes" : [
ObjectId("561f9c319cdd94401ae160ef")
],
"text" : "This is the answer content",
"question" : {
"questionId" : ObjectId("56334031e45552c018fc4757"),
"sender" : ObjectId("561f9c319cdd94401ae160ef"),
"date" : ISODate("2015-10-30T10:02:25.323Z")
},
"date" : ISODate("2015-10-30T10:12:37.894Z")
},
{
"_id" : ObjectId("563246cfa04c1b281b6d97bf"),
"author" : ObjectId("561f9c319cdd94401ae160ef"),
"likeCount" : 0,
"likes" : [],
"text" : "Bla bla bla",
"question" : {
"questionId" : ObjectId("562f9a74a16d6fb418752b37"),
"sender" : ObjectId("561f9c319cdd94401ae160ef"),
"date" : ISODate("2015-10-27T15:38:28.337Z")
},
"date" : ISODate("2015-10-29T16:18:23.020Z")
}
【问题讨论】: