美文网首页
mongdb 高级查询

mongdb 高级查询

作者: 喊我小王吧 | 来源:发表于2018-12-14 22:25 被阅读0次

    mongdb 高级查询

    条件查询

    • (>) 大于 - $gt
    • (<) 小于 - $lt
    • (>=) 大于等于 - $gte
    • (<= ) 小于等于 - $lte

    大于4

    db.user.find({"age":{$gt:4}})
    

    大于等于4

    db.user.find({"age":{$gte:4}})
    

    小于

    db.user.find({"age":{$lt:4}})
    

    小于等于

    db.user.find({"age":{$lte:5}})
    

    多个条件查询

    4<age<10

    db.user.find({"age":{$gt:4,$lt:10}})
    

    查询不等3的数据

    db.user.find({"age":{$ne:3}});
    

    in 包含

    查找在某个范围内的 查询结果符合 其中的某一个值即可

    db.user.find({"age":{$in:[1,2,3]}});
    

    nin 不包含

    db.user.find({"age":{$nin:[1,2,3]}})
    

    all

    必须满足[ ] 内每一个值

    db.user.find({"age":{$all:[1,2,3]}});
    

    模糊查询

    基础语法:{“key”:正则标记}; 例如:db.user.find ( { "name":/s/i } )

    i 忽略大小写

    db.user.find({"name":/s/i})

    { "_id" : NumberLong(1), "name" : "zs", "grade" : "111", "age" : 1, "_class" : "com.example.mongdbtest.demo.pojo.User" }
    { "_id" : NumberLong(2), "name" : "LS", "grade" : "22", "age" : 2, "_class" : "com.example.mongdbtest.demo.pojo.User" }
    { "_id" : NumberLong(4), "name" : "lsit2", "_class" : "com.example.mongdbtest.demo.po
    
    不忽略大小写:

    db.user.find({"name":/s/})

    { "_id" : NumberLong(1), "name" : "zs", "grade" : "111", "age" : 1, "_class" : "com.example.mongdbtest.demo.pojo.User" }
    { "_id" : NumberLong(4), "name" : "lsit2", "_class" : "com.example.mongdbtest.demo.pojo.User" }
    { "_id" : NumberLong(3), "name" : "lsit1", "age" : 12, "_class" : "com.example.mongdb
    
    完整语法:

    { key : {"$regex" : 正则标记, "options" : 选项 } }

    db.user.find({"name":{"regex":"7","options":"$i"}})

    { "_id" : NumberLong(11), "name" : "lsit47", "age" : 11, "_class" : "com.example.mongdbtest.demo.pojo.User" }
    { "_id" : NumberLong(21), "name" : "lsit417", "age" : 21, "_class" : "com.example.mongdbtest.demo.pojo.User" }
    

    或者

    db.user.find({"name":{"regex":/7/,"options":"$i"}})

    { "_id" : NumberLong(11), "name" : "lsit47", "age" : 11, "_class" : "com.example.mongdbtest.demo.pojo.User" }
    { "_id" : NumberLong(21), "name" : "lsit417", "age" : 21, "_class" : "com.example.mongdbtest.demo.pojo.User" }
    
    相应的正则标记不加引号

    |- 对于options主要是设置正则的信息查询的标记
    ​ |- "i" 忽略字母大小写
    ​ |- "m" 多行查找
    ​ |- "x" 空白字符除了被转义的或在字符类中以外的完全被忽略
    ​ |- "s" 匹配所有的字符,包括换行内容
    |- 对于 i 和 m 可以直接使用,但是 x 和 s 必须使用 "$regex"


    排序:

    db.user.find({"name":{"regex":/s/,"options":"/i"}}).sort({"age":-1})

    执行完查询后结果集进行排序。
    • -1 表示倒叙
    • 1 表示正序

    查询 title 包含"教"字的文档:

    db.col.find({title:/教/})
    

    查询 title 字段以"教"字开头的文档:

    db.col.find({title:/^教/})
    

    查询 titl e字段以"教"字结尾的文档:

    db.col.find({title:/教$/})
    

    【distinct】

    相当于sql中的 distinct 也表示去除重复

    返回一个数组,包含所有的键

    去重 返回的不重复的所有数据
     db.user.distinct("name");
    
    // 1
    [
        "zs",
        "ls",
        "教1s",
        "教2sS",
        "张教S",
        "Abc张教d",
        "王五1",
        "LS",
        "zs2",
        "王五4",
        "王五5",
        "王五6",
        "王五8",
        "王五9",
        "tizzY",
        "tizzy",
        "tiZZy",
        "王五13",
        "王五14",
        "王五15",
        "ls3",
        "new",
        "ls2"
    ]
    
    

    where查询

    查询所有

    db.user.find();
    

    查询 age > 5

    db.user.find({$where:"this.age > 5"})
    

    count函数

    查询记录条数

    db.user.find().count()
    

    或者

    db.user.count()
    

    count() 查询

    db.user.count({"age":3})
    

    count() 里面 带参数查询和不带参数查询效率差别很大

    db.user.find().skip(0).limit(10);

    返回总数
    实际上返回的是所有的数据总数:
    db.user.find().skip(10).limit(2).count()
    
    应该这么写返回查询后的数
    db.user.find().skip(10).limit(2).count(true)
    

    exists

    查询出 age为 null的数据

    db.user.find({"age":null});  
    

    结果:前两个文档没有age字段但是也查询出来了,第三个是符合我们查询的结果。

    // 1
    {
        "_id": NumberLong("4"),
        "name": "lsit2",
        "_class": "com.example.mongdbtest.demo.pojo.User",
        "grade": 3
    }
    
    // 2
    {
        "_id": 25,
        "name": "ls4",
        "id": 25
    }
    
    // 3
    {
        "_id": ObjectId("5c1371e4a9aa1125b8003a85"),
        "name": "new",
        "age": null,
        "id": "24"
    }
    

    这时候应该用存在 exists 字段 过滤一下,确认age字段是否存在!

    查询出age 字段为 null的值数据
    db.user.find({"age":{$exists:true,$in:[null]}}); 
    

    age字段存在的结果

    // 1
    {
        "_id": ObjectId("5c1371e4a9aa1125b8003a85"),
        "name": "new",
        "age": null,
        "id": "24"
    }
    
    
    

    查询出字段不存在 ,切值为 null的 数据

    db.user.find({"age":{$exists:false,$in:[null]}});
    

    age字段不存在的结果

    // 1
    {
        "_id": NumberLong("4"),
        "name": "lsit2",
        "_class": "com.example.mongdbtest.demo.pojo.User",
        "grade": 3
    }
    
    // 2
    {
        "_id": 25,
        "name": "ls4",
        "id": 25
    }
    

    分页查询

    skip 跳过多少数量 limit 返回多少数量

    db.user.find({"age":{$gt:4}}).skip(0).limit(2);   //相当于显示第一页 每页2条数据
    db.user.find({"age":{$gt:4}}).skip(2).limit(2);   //相当于显示第二页 每页2条数据
    

    sort 排序

    1 表示升序 -1 表示降序

    db.user.find({"age":{$gt:4}}).sort({"name":1}); 
    

    相关文章

      网友评论

          本文标题:mongdb 高级查询

          本文链接:https://www.haomeiwen.com/subject/kvbnhqtx.html