mongodb 查询

1、模糊查询

通过模糊查询,查找相关数据:
db.test.find({name:/joe/})    ---查询name字段含有joe的数据 等同于db.test.find({name:{$regex:'joe'}})
db.test.find({name:/joe/i})   加了i,那么就不会区分大小写,都会显示,等同于db.test.find({name:{$regex:'joe',$options:'i'}})
db.test.find({name:/^joe/i})   ^匹配以joe开头的,而且不区分大小写

2、类型查询

 
$type使用:
> db.test.find({content:{$type:2}}) --显示content是string的数据
> db.test.find({content:{$type:1}})  --显示content为double类型的
下面给出mongodb基于bson类型的列表:
Type DescriptionType value
Double1
String2
Object3
Array4
Binary data5
Object id7
Boolean8
Date9
Null10
Regular expression11
JavaScript code13
Symbol14
JavaScript code with scope15
32-bit integer16
Timestamp17
64-bit integer18
Min key255
Max key127

3、条件查询

根据以下数据

db.dev.find({"dev":"测试"})

4、mongodb查询中的null和存在不存在

查询集合c中y的值为null或者不存在

>db.c.find( { “y” : null } )

 

查询集合c中y的值为null,(仅返回y的值为null的数据,不会返回不存在的)

>db.c.find( { “y” : { $type : 10 } } )

还有一种写法如下

>db.c.find({“y”:{“$in”:[null], “$exists”:true}})

 

查询集合c中y的值不存在(不会返回y的值为null的数据)

>db.c.find( { “y” : { $exists : false } } )

 

查询集合c中y的值不为null且存在的记录

 

>db.c.find( { “y” : {"$ne":null, $exists: true }} )

或者:>db.c.find( { “y” : {"$ne":null }} )

 5、ff

转载于:https://www.cnblogs.com/michellexiaoqi/p/7575591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值