1 没有query的情况

上篇文章介绍到mongodb的基本查询语法为:
db.collection.find(query, projection).sort("XXX",-1).skip(pageNum).limit(pageSize)
但是同时也说到query,projection,sort,skip和limit都是非必填的。那都不填就成了这样

find() 或 db.users.find({}) 或 db.users.find({},{})

其实这都是一个意思,就是表示没有特定的查询条件,也就是查询所有的数据。


2 根据子文档查询

mongo里一条数据就是一个文档,而一个文档又可看成好多文档组成的一个集合。根据子文档查询当前文档是mongo查询里几乎最简单的,下面分几种情况来介绍一下:

  • 子文档为一个键值对时—》可以翻译为查询所有文档里子文档含有{“username”:“lison”}的文档
db.users.find({"username":"lison"})
  • 子文档为数组时 —》注意,只有子文档含有该数组顺序一致的才会被匹配上
db.users.find ({ "favorites.movies" : ["杀破狼2", "战狼", "雷神1"]})
  • 子文档为对象时
db.users.find({"address" : {"aCode" : "411000", "add" : "长沙" }})
  • 有多个子文档时 —》 查询同时包含这些子文档的文档,相当于and
db.users.find({"username" : "lison", "country" : "china","favorites.movies" : ["杀破狼2", "战狼", "雷神1"]})
3 根据特定条件进行查询

特定条件即大于,小于,包含,不包含等,这里总结了下面辣么多,本文将摘几个做简单演示。

运算符类型 运算符 描述
范围 $eq 等于
$lt 小于
$gt 大于
$lte 小于等于
$gte 大于等于
$in 判断元素是否在指定的集合范围里
$all 判断数组中是否包含某几个元素,无关顺序
$nin 判断元素是否不在指定的集合范围里
布尔运算 $ne 不等于,不匹配参数条件
$not 不匹配结果
$or 有一个条件成立则匹配
$nor 所有条件都不匹配
$and 所有条件都必须匹配
$exists 判断元素是否存在
其他 . 子文档匹配
$regex 正则表达式匹配
  • 查询年龄在10~20岁之间的人
db.users.find({"age" :{"$gte":10,"$lte":20} })
  • $in —> 查询姓名为lison、lucy这个范围的人
db.users.find({"username":{"$in":["lison","lucy"]}})
  • $and —> 查询username为lison且年龄为18的人
db.users.find({"$and":[{"username":"lison"},{"age":18}]})
等价于: db.users.find({"username":"lison","age":18}) 所以我很少用$and
  • $or —> 查询所有length大于1.77或者length不存在的人 —> 会把两个文档都查出来
db.users.find({"$or":[{"length":{"$gt":1.77}},{"length":{"$exists":false}}]})
  • 正则表达式 —> 查询用户名里包含so的用户
db.users.find( { "username" : /.*so.*/i});
  • $not(需要特别注意) —> 查询length大于1.77的人或者没有length这个字段的人 —> 会把两个文档都查出来
db.users.find({"length":{"$not":{"$lte":1.77}}})

注意: not会把不包含查询语句字段的文档也检索出来,因此上面的sql相当于下面这条sql

db.users.find({"$or":[{"lenght":{"$gt":1.77}},{"lenght":{"$exists":false}}]})

  • $exists(比较重要,用法也比较多) —> 查询用户资料中有country字段的人
db.users.find({"country":{"$exists":true}}) //注意:country为null或者为""都可以被检索出来

如果不想要有country字段,但其值为null或""的可以这么做:

db.users.find({"country":{"$exists":true},"country":{"$nin":[null,""]}})

这里顺带说一下null
假如我想查country为null的,下面的语句是有问题的,因为它会把country为null和没有country的数据都查到

db.users.find({"country":null})

那该怎么搞呢?可以写成下面的样子:

db.users.find({"country":{"$in":[null],"$exists":true}})

经我测试发现下面的语句也行====这里的and好像不能省略

db.users.find({"$and":[{"country":null},{"country":{"$exists":true}}]})

但下面的语句不行 — 下面的语句执行获得结果与预想的不一致

db.users.find({"country":null,"country":{"$exists":true}})

再顺带多说一个用法 — 判断数组是否为空
查询movies数组不为空的文档 —> 判断movies集合中的第一个值是否存在,如若不存在,说明该集合为空集合

db.users.find({"favorites.movies.0":{"$exists":true}})

本文测试数据
{ 
    "_id" : ObjectId("5d444851a7d9c6d42839968d"), 
    "username" : "lison", 
    "country" : "china", 
    "address" : {
        "aCode" : "411000", 
        "add" : "长沙"
    }, 
    "favorites" : {
        "movies" : [
            "杀破狼2", 
            "战狼", 
            "雷神1"
        ], 
        "cites" : [
            "长沙", 
            "深圳"
        ]
    }, 
    "age" : 18.0, 
    "salary" : NumberDecimal("18889.09"), 
    "length" : 1.79
}
{ 
    "_id" : ObjectId("5d444851a7d9c6d42839968e"), 
    "username" : "james", 
    "country" : null, 
    "address" : {
        "aCode" : "311000", 
        "add" : "地址"
    }, 
    "favorites" : {
        "movies" : [

        ], 
        "cites" : [
            "西安", 
            "东京", 
            "上海"
        ]
    }, 
    "age" : 24.0, 
    "salary" : NumberDecimal("7889.09"), 
    "height" : 1.35
}
{ 
    "_id" : ObjectId("5d44768557cc070b44a12217"), 
    "username" : "yoyo", 
    "country" : ""
}
{ 
    "_id" : ObjectId("5d44797257cc070b44a12226"), 
    "username" : "nrsc"
}