MongoDB查询的and和or组合

3

这个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中的等效语句是什么?Mongo中是否存在可以合并and和or的语句?

1个回答

6

是的,你可以将它们结合起来使用。你应该能够执行以下操作:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接