feed = coll.aggregate([
{'$project' :{'Upvoted' :
{'Upvoters' : {'$in' : [ObjectId(userid)]} }}},
{'$match' :{ '$or': [ {"_id": ObjectId(userid) }, {"Friends": ObjectId(userid) } ] } },
{'$unwind' : "$Wall" },
{'$sort' : { "Wall.Hotness" : -1 }},
{'$limit' : numResults }] )
我正在尝试预测用户是否获得了赞成票。我通过检查用户是否在向上投票者列表中来做到这一点。It错误提示当前操作符$in无效。在mongo中有返回布尔值的in的替代方案吗?
发布于 2013-04-26 21:08:14
尝试将$in
与$project
一起使用时出现错误,因为$in
仅是$match
管道阶段的有效运算符。
我不认为您的聚合实际上需要一个显式的“布尔值”:您只想匹配该属性为true的所有文档。
聚合示例:
var currentUser = ObjectId("517ae8124bc9ade96ca6403f");
var numResults = 10;
db.feed.aggregate(
// Find all posts that currentUser has upvoted
{ $match: {
'Upvoters' : currentUser
}},
// Unwind the wall messages
{ $unwind : "$Wall" },
// Sort by hotness (descending)
{ $sort : { "Wall.Hotness" : -1 } },
// Limit results
{ $limit : numResults }
)
https://stackoverflow.com/questions/16240382
复制相似问题