MongoDB查询组合和&要么

时间:2013-06-27 03:40:58

标签: sql mongodb

此SQL查询:

SELECT name
FROM user 
WHERE user.provider = "google" or user.email="email@example.com"

具有等效的mongodb查询:

db.user.find({
    "$or": [{
        "user.provider": "google"
    }, {
        "user.email": "email@example.com"
    }]
}, {
    "name": 1
});

这个怎么样?

SELECT name
FROM user 
WHERE (user.provider = "google" and user.id="1") or user.email="email@example.com"

上面的SQL到mongo的等价物是什么?是否有可能结合和&还是在蒙戈?

1 个答案:

答案 0 :(得分:6)

是的,你可以将它们结合起来。你应该能够做到:

"$or":[
  {"$and": [{"user.provider": "google"}, {"user.id": "1"}] },
  { "user.email" : "email@example.com" }
]