Mongodb操作之查询(循序渐进对比SQL语句)

本文详细介绍了MongoDB的基本查询方法及操作符用法,包括集合查询、返回指定键值、条件过滤、排序与限制返回条目数、计数、大于、大于等于、小于、小于等于、不等于、空查询、匹配所有、数组查询、在范围内、不在范围内、至少包含、至少不包含、找出不匹配、或匹配、存在性查询、取模、正则匹配、返回数组元素个数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工具推荐:Robomongo,可自行百度寻找下载源,个人比较推荐这个工具,相比较mongoVUE则更加灵活。

 

集合简单查询方法

mongodb语法:db.collection.find()  //collection就是集合的名称,这个可以自己进行创建。

对比sql语句:select * from collection;

查询集合中所有的文档,即关系型数据库中的查询表中的所有数据。

 

返回制定的键值

mongodb语法:db.collection.find({},{"userid":1})

对比sql语句:select userid from collection;

 

条件过滤

mongodb语法 : db.collection.find({"userid":495})

对比sql语句:select * from collectionwhere userid = 495;

 

  

查询全格式书写解释

  db.collection.find({},{})

第一个{}中,写入的都是相当于sql语句中where后的条件,多条件格式为{"key":value,"key2":"value2"}

第二个{}中,写入的都是相当于sql语句中跟在select后边的具体字段,格式为{"key":value,"key2":value}

      当value = 0时为不显示此字段值,当value !=0,即等于任何非0值时,则为显示此字段。

例:

mongodb查询:

    db.error.find({"userid":1,"type":"debug"},{"userid":1,"type":1,"myssage":1})

sql查询:

    select userid,type,message from error where userid=1 and type = "debug";

 

sort排序与limit返回固定条目数

mongodb查询:

    db.collection.find({"userid":1,"type":"debug"},{"userid":1,"type":1,"myssage":1}).sort("time":-1).limit(10)

sql查询:

    select userid,type,message from collection where userid=1 and type = "debug" order by time desc limit 10;

 

count返回结果集总数

mongodb查询:

    db.collection.count()

sql查询:

    select count(*) from collection;

 

查询操作符"$gt" -->大于操作符

mongodb查询:

    db.collection.find({"userid":{"$gt":"494"}})

sql查询:

    select * from collection where userid > 494;

 

查询操作符"$gte" -->大于等于

mongodb查询:

    db.collection.find({"userid":{"$gte":"494"}})

sql查询:

     select * from collection where userid >= 494;

 

查询操作符 "$lt"-->小于

mongodb查询:

    db.collection.find({"userid":{"$lt":"494"}})

sql查询:

     select * from collection where userid <494;

 

 

查询操作符"$lte"-->小于等于

mongodb查询:

    db.collection.find({"userid":{"$lte":"494"}})

sql查询:

     select * from collection where userid < =494;

 

 

查询操作符"$ne"-->不等于

mongodb查询:

    db.collection.find({"userid":{"$ne":"494"}})

sql查询:

     select * from collection where userid != 494;

 

 

查询操作符"null查询"-->空

mongodb查询:

    db.collection.find({"userid":null})

sql查询:

     select * from collection where userid is null;

 

查询操作符"$all"-->匹配所有

mongodb查询:

        db.collection.find({"userid":{"$all":"494"}})

sql查询:

     select * from collection where userid = 494;

 

    当文档类型为数组时,使用$all进行匹配,非数组类型使用时与单一匹配一样。


查询操作符"$size"-->用于数组查询,查询指定长度的数组

mongodb查询:

        db.collection.find({"remark":{"$size":"3"}})

 

查询操作符"$in"--> 在范围内

mongodb查询:

        db.collection.find({"userid":{"$in":["297","495"]}})

sql查询:

     select * from collection where userid in (297,495);

 


查询操作符"$nin"-->不在范围内

mongodb查询:

        db.collection.find({"userid":{"$nin":["297","495"]}})

sql查询:

     select * from collection where userid not in (297,495);


查询操作符"$and"-->至少包含两个表达式,两个表达式都满足的文档返回

mongodb查询:

        db.collection.find({"$and":[{"userid":"495"},{"type":"info"}]})

sql查询:

     select * from collection where userid=495 and type='info';

 


查询操作符"$nor"-->至少包含两个表达式,两个表达式都不满足的文档返回

mongodb查询:

        db.collection.find({"$nor":[{"userid":"495"},{"userid":"297"}]})

sql查询:

     select * from collection where userid not in (297,495);

 


查询操作符"$not"-->找出不匹配表达式的文档,不能够单独使用,必须与其他表达式配合使用

mongodb查询:

        db.collection.find({"userid":{"$not":{"$gt":"297"}}})

        等同于:db.collection.find({"userid":{"$lte":"297"}}})

sql查询:

     select * from collection where userid <=297;



查询操作符"$or"-->至少包含两个表达式,两个表达式至少满足一个的文档返回

mongodb查询:

        db.collection.find({"$or":[{"userid":"495"},{"userid":"297"}]})

sql查询:

     select * from collection where userid =297 or userid = 495;


查询操作符"$exists"-->查询文档中字段是否存在

mongodb查询:

        db.collection.find({"$exists":"true"})


查询操作符"$mod"-->键值对变量参数取模,值等于另一个参数

mongodb查询:

        db.collection.find({"userid":{"$mod":[10,7]}})

        执行条件:userid字段值必须是数字,userid对10取模,值等于7的文档返回。

sql查询:

     select * from collection where (user_id%10)=7

        

查询操作符"$regex"-->正则匹配

 

mongodb查询:

        db.collection.find({"userid":/5$/})

sql查询:

        select * from collection where userid like '%5';

  sql正则写法:      
     select * from collection where userid regexp ".5$";
 
   正则匹配userid的最后一位是5的,sql中只有使用regexp才可以使用复杂的正则表达式,使用Like的方式不可以进行复杂的正则匹配

 

查询操作符"$slice"-->控制返回的数组元素中的元素个数

mongodb查询:

        db.collection.find({},{"remark":{"$slice":5})

                    remark数组键值,返回数组键值中的前5个元素

        db.collection.find({},{"remark":{"$slice":[10,5]})

                    remark数组键值,返回数组键值中的第11到15个元素,偏移量为10,然后返回5个。

        db.collection.find({},{"remark":{"$slice":-5})

                    remark数组键值,返回数组键值中的后5个元素

 

 逐渐补充中......

 

转载于:https://www.cnblogs.com/lingfengblogs/p/4195984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值