关闭

MongoDB小结17 - find【查询条件$or】

标签: MongoDB
11501人阅读 评论(0) 收藏 举报
分类:

我们再添加一个游泳的人,并用$in查询游泳的人

db.user.find({"hobby":{"$in":["swimming"]}},{"_id":0})
{ "hobby" : "swimming", "gender" : "female" }
{ "hobby" : "swimming", "gender" : "male" }

$in 是对单个键进行的查询,用$or查询可以匹配多个键

db.user.find({"$or":[{"hobby":"swimming"},{"gender":"female"}]},{"_id":0})
{ "hobby" : "swimming", "gender" : "female" }
{ "hobby" : "swimming", "gender" : "male" } 

现在,我们把查询条件的female改成male

db.user.find({"$or":[{"hobby":"swimming"},{"gender":"male"}]},{"_id":0})
{ "hobby" : "swimming", "gender" : "female" }
{ "hobby" : "dancing", "gender" : "male" }
{ "hobby" : "singing", "gender" : "male" }
{ "hobby" : "singing", "gender" : "male" }
{ "hobby" : "dancing", "gender" : "male" }
{ "hobby" : "swimming", "gender" : "male" }

现在我们可以得出结论,OR查询($in 和 $or)是尽可能的获取更多的匹配项。 OR查询其实是取并集,满足其中一条及以上,即可被查询到。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2441350次
    • 积分:14983
    • 等级:
    • 排名:第747名
    • 原创:148篇
    • 转载:5篇
    • 译文:0篇
    • 评论:14条
    文章推荐
    最新评论