2011-08-09 24 views
3

我在此查询中使用多个运算符。我没有得到任何语法错误,但我没有收到任何数据(应该是),所以我非常确定我正在查看逻辑错误。问题是,我没有看到它。AND和OR在Mongo查询

查询:

db.questions.find(
    {'$and': [ 
     {'answers.s_user_id': {'$ne': s_user_id}}, 
     {'$or': [ 
       {'s_text': re.compile(s_term, re.IGNORECASE)}, 
       {'choices': re.compile(s_term, re.IGNORECASE)} 
     ]} 
    ]} 
) 

任何提示赞赏。

回答

6

我刚刚发现的问题,因为这里记载:https://jira.mongodb.org/browse/SERVER-2585

嵌套$或之前,不支持的MongoDB 1.9.1截至2011年7月24日。我在旧版本。

+1

要添加到您的发现,你的查询等效于db.questions.find( { 'answers.s_user_id':{ '$ NE':s_user_id}},{ $或:[ {” s_text':re.compile(s_term,re.IGNORECASE)}, {'choices':re.compile(s_term,re.IGNORECASE)} ]} } ) – snez

1
Please find the query 
AND Queries With Multiple Expressions Specifying the Same Operator 

Consider the following example: 

db.inventory.find({ 
    $and : [ 
     { $or : [ { price : 0.99 }, { price : 1.99 } ] }, 
     { $or : [ { sale : true }, { qty : { $lt : 20 } } ] } 
    ] 
})