ITPub博客

首页 > 数据库 > NoSQL > Mongodb 6.0读取文档

Mongodb 6.0读取文档

原创 NoSQL 作者:z597011036 时间:2023-09-15 16:11:57 0 删除 编辑

1.简单匹配查询

db.tong.find();        #读取tong表中全部数据

db.tong.find().pretty();     #读取的数据用json格式显示

db.tong.find({name:"zhangsan"});      #查询name字段为 zhangsan的数据

db.tong.find({name:"zhangsan",age:'100'});    #查询name字段为 zhangsan,age值为100的数据


2.比较操作符

$eq      值相等

$ne      值不相等

$gt      值大于

$gte    值大于和等于

$lt       值小于

$lte     值小于和等于

$in      包含

$nin    不包含

db.tong.find({name:{$eq:'lisi'}});     #读取name字段等于lisi的值

db.tong.find({name:{$eq:"zhangsan"},age:{$ne:"200"}});    #读取name字段等于zhangsan,age字段不等于200的数据

db.tong.find({age:{$gt:"200"}});     #读取age字段大于200的数据(整型类型比大小,字符串类型值比字符大小)

db.tong.find({age:{$in:["200","24"]}});      #读取age字段包含200或24的值

db.tong.find({age:{$nin:["200","24"]}});    #读取age字段不包含200或24的值


3.逻辑操作符

$not     条件匹配不成立

$and    多个条件都匹配

$or       至少一个条件匹配

$nor     多个条件全部不成立

db.tong.find({age:{$not:{$lt:"100"}}})      #读取age字段值不小于100的数据(大于100的值显示)

db.tong.find({age:{$not:{$lt:"100"}},age:{$not:{$gt:"150"}}})     #读取age字段不小于100,不大于150的数据(读取100-150之间的值)

db.tong.find({age:{$gt:'100',$lt:'200'}})    #读取age字段大于100和小于200的数据

db.tong.find({$and:[{name:'lisi'},{age:'200'}]})     #读取name字段为lisi和age字段为200的值(两个条件都成立)

db.tong.find({$or:[{name:'lisi'},{name:'zhangsan'}]})    #读取name字段为lisi或zhangsan的数据(条件可以成立一个)

db.tong.find({$nor:[{name:'lisi'},{age:'100'}]})     #读取name字段不为lisi和age不是100的数据(所有条件都不成立)


4.字段操作符

$exists    是否存在

$type      类型

db.tong.find({number:{$exists:true}})     #读取文档中存在number字段的数据

db.tong.find({regin:{$exists:false}})        #读取文档中不存在regin字段的数据

db.tong.find({number:{$type:'int'}})       #读取文档中number字段是整型的数据

db.tong.find({number:{$type:'string'}})  #读取文档中number字段是字符串的数据


5.数组操作符

$all     匹配字段所有文档

$elemMatch   至少满足一个条件

db.accounts.find({contact:{$all:["China","Beijing"]}})    #读取文档中contact字段同时出现China和Beijing的数据(两个值都存在)

db.accounts.find({contact:{$elemMatch:{$gt:"10000000",$lt:"20000000"}}})  #读取contact字段中10000000与20000000之间的数据


6.运算操作符

与$in一起使用,/pattern/<options>

db.tong.find({name:{$in:[/^q/,/l/]}})      #读取文档中name字段以q和l开头的数据

读取不区分大小写的数据

db.tong.find({name:{$regex:/qqQ/,$options:'i'}})      #读取文档中name字段包含qqq的数据,不含大小写


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25854343/viewspace-2983760/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Mysql数据库安全
请登录后发表评论 登录
全部评论

注册时间:2014-03-31

  • 博文量
    79
  • 访问量
    486678