Mongo查询中的AND和OR

2024-04-19 14:20:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这个查询中使用了多个运算符。我没有发现语法错误,但是我没有得到任何数据(应该是这样),所以我很确定我看到的是一个逻辑错误。问题是,我看不出来。在

查询:

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)}
        ]}
    ]}
)

如有任何提示,不胜感激。在


Tags: and数据reiddb错误运算符逻辑
2条回答

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

直到2011年7月24日,MongoDB 1.9.1才支持嵌套$OR。我用的是老版本。在

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 } } ] }
    ]
} )

相关问题 更多 >