如何在mongodb中将$ in运算符投影为布尔值? [英] How to project the $in operator as a boolean in mongodb?
本文介绍了如何在mongodb中将$ in运算符投影为布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 }] )
我正在尝试预测用户是否已投票。我通过检查用户是否在支持者列表中来做到这一点。错误显示当前无效的运算符$ in。
I'm trying to project whether a user has upvoted or not. I do this by checking if a user is in the upvoters list. It errors saying invalid opererator $in currently. Is there an alternative to in that returns a boolean in mongo?
推荐答案
您是否在尝试使用<$ c时遇到错误? $ c> $ in 和 $ project
,因为 $ in
只是有效的运算符为 $ match
流水线阶段。
You're getting an error trying to use $in
with a $project
because $in
is only a valid operator for a $match
pipeline stage.
我认为您实际上不需要显式的布尔值汇总:您只想匹配此属性为true的所有文档。
I don't think you actually need an explicit "boolean" for your aggregate: you just want to match all documents where this property is 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 }
)
这篇关于如何在mongodb中将$ in运算符投影为布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文