源数据
{
"uuid" : 12700004,
"card" : 981.3000000000029, "weixin" : { "unionId" : "o1wS90l6kHDFXY2lAPDb9bPxRZxI1", "openId" : "o9zCJ1Aat93B0UjdlCFDAWmimBcEb", "nick" : "麦袋", "sex" : 1, "avatar" : "http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoNcMa5gLGs9iaTYCmNqycSk5XUWzKDVfj6sPkk1Oia0YSy3SmMrcX3kxsvEu4UfiblckRo7CpM0xicKg/1321" }, "liaobe" : null, "mobile" : { "number" : "18874819762" }, "visitor" : null, "accountType" : 1, "mobileRewardReceived" : true }
MongoDB 查询数据的语法格式如下:
db.collection.find(query, projection)
query :可选,使用查询操作符指定查询条件
projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)
一、查询所有数据
显示所有列
db["user"].find() # MongoDB写法 db["user"].find().pretty() # 是find出的数据在命令行中更加美观显示,不至于太紧凑,同select * from user\G; select * from user;
显示指定列
db["user"].find({},{"liaobe":1,"mobile":1}).pretty() # 如果projection不为空,不需要查询条件时,必须键入{}
# projection 指定哪些列显示和不显示 (0:不显示 1:显示) select liaobe,mobile from user\G;
查询json数据二级数据
"mobile" : { "number" : "18874819762" },
以上数据,要查询number=18874819762的数据
db["user"].find({"mobile.number":"18874819762"}).pretty() # 一级.二级.三级,依次类推
二、等于条件查询
db["user"].find({"accountType" : 1}).pretty()
select * from user where accountType=1\G;
三、and
db["user"].find({"uuid" : 12700004,"accountType" : 1,"authType" : 2}).pretty() # 多个条件用, 隔开
select * from user where uuid=12700004 and accountType=1 and authType=2\G;
四、or
db["user"].find( { '$or':[ {"mobile.number":"18874819762"}, {"weixin.unionId":"o1wS90vp2ENW2Vu-QTnHlcrdU5kA"} ] }, {"weixin.nick":1} ).pretty()
select nick from user where number='18874819762' or unionId='o1ws90vp2ENW2Vu-QTnH1crdU5kA'
五、< <= > >= (lt lte gt gte)
db["user"].find({'card':{$lt:5}}).pretty() # select * from user where card < 5\G; db["user"].find({'card':{$gt:0,$lt:5}},{"card":1}).pretty() # select card from user where card > 0 and card <5\G; db["user"].find({'card':{$lte:5}},{"card":1}).pretty() # select card from user where card <=5;\G; db["user"].find({'card':{$gte:0,$lte:5}},{"card":1}).pretty() # select card from user where card >=0 and card <=5\G;
六、使用in,not in (in,nin)
db["user"].find({"uuid":{$in:[83405282,25594661]}},{"uuid":1,"card":1}).pretty() # select uuid,card from user where uuid in(83405282,25594661)\G; db["user"].find({"authType":{$nin:[2,1]}},{"uuid":1,"card":1,"authType":1}).pretty() # select uuid,card,authType from user where authType not in(2,1)\G;
七、匹配null
db["user"].find({"liaobe":null},{"uuid":1,"weixin.nick":1,"liaobe":1}).pretty() # select uuid,nick,liaobe from user where liaobei is null\G;
八、like(MongoDB支持正则表达式)
db["user"].find({"weixin.nick":/^随梦/},{"uuid":1,"weixin.nick":1}).pretty() # select uuid,nick from user where nick like '随梦%'\G; db["user"].find({"weixin.nick":/袋$/},{"uuid":1,"weixin.nick":1}).pretty() # select uuid,nick,from user where nick like '%袋'\G;
九、distinct去重
db["user"].distinct("weixin.nick",{"weixin.nick":/^随梦/}) # select distinct(nick) from user where nick like "随梦%";
十、count统计
db["user"].find({"card":{$lt:5}}).count() # select count(*) from user where card <5;
db.runCommand( { distinct: "user", key: "weixin.nick", query: { card: {"$lt":5}} } ).values.length; # 先通过find查出数据,然后去重,最后统计
十一、排序
在 MongoDB 中使用 sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。
db["user"].find({"card":{$gt:10,$lt:20}},{"uuid":1,"weixin.nick":1,"card":1}).sort({"card":1}) # select uuid,nick,card from user where card>10 and card<20 asc; db["user"].find({"card":{$gt:10,$lt:20}},{"uuid":1,"weixin.nick":1,"card":1}).sort({"card":-1}) # select uuid,nick,card from user where card>10 and card<20 desc;
十二、limit
如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数。
db["user"].find({"card":{$gt:10,$lt:20}},{"uuid":1,"weixin.nick":1,"card":1}).sort({"card":1}).limit(5)