2011-08-09 78 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 } } ] } 
    ] 
})