我们再添加一个游泳的人,并用$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查询其实是取并集,满足其中一条及以上,即可被查询到。