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
Double 1
String 2
Object 3
Array 4
Binary data 5
Object id 7
Boolean 8
Date 9
Null 10
Regular expression 11
JavaScript code 13
Symbol 14
JavaScript code with scope 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255
Max key 127

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