MongoDB小结16 - find【查询条件$in】

$in可以查询一个键的多个值 举例,每个人有爱好,假定为一个,数据太多,咱们用第二个参数来过滤一下

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

我们想查询喜欢dancing和swimming和的人,可以得到如下结果

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

若只查询会跳舞的人

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

既然$in,那么与之相对的就$nin,可以查询到不包括指明信息的文档

你可能感兴趣的:(mongodb)