1、 查询的基本语法
db.表明.find({},{});
第一个{}是条件的bson数据,如果有多个条件,以”,”分割如:
>db.product.find({"id":"3"},{});
{ "_id" :ObjectId("57182150560ca68a25e2991e"), "id" : "3","name" : "钢笔", "price" : 6 }
如果返回所有字段,第二个参数可以写成空的{},也可以不写:如db.product.find({"id":"3"});
第二个{}是要返回的字段的bson格式,格式为{ 字段名:n },如果n为1,则返回该字段的值,如果n为0,则不返回该字段地址,返回多个字段,以“,“分割,如:
>db.product.find({"id":"3"},{name:1});
{ "_id" : ObjectId("57182150560ca68a25e2991e"),"name" : "钢笔" }
>db.product.find({"id":"3"},{name:1,_id:0});
{ "name" : "钢笔" }
如果需要查询所有,则find的两个参数都不用填写:db.product.find();
2、 查询操作的比较运算操作符
数据情况
>db.product.find();
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
{ "_id" :ObjectId("57182ae9560ca68a25e29921"), "id" : "4","name" : "签字笔2", "price" : 5 }
$lt:小于
>db.product.find({"price":{$lt:5}});
{ "_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
$lte:小于等于
>db.product.find({"price":{$lte:5}});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
{ "_id" :ObjectId("57182ae9560ca68a25e29921"), "id" : "4","name" : "签字笔2", "price" : 5 }
$gt:大于
>db.product.find({"price":{$gt:5}});
{ "_id" :ObjectId("57182150560ca68a25e2991e"), "id" : "3","name" : "钢笔", "price" : 6 }
$gte:大于等于
> db.product.find({"price":{$gte:5}});
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
{ "_id" :ObjectId("57182ae9560ca68a25e29921"), "id" : "4","name" : "签字笔2", "price" : 5 }
$ne:不等于
>db.product.find({"price":{$ne:5}});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
{ "_id" :ObjectId("57182150560ca68a25e2991e"), "id" : "3","name" : "钢笔", "price" : 6 }
组合使用:
>db.product.find({"price":{$lt:6,$gt:4}});
{ "_id" :ObjectId("57182ae9560ca68a25e29921"), "id" : "4","name" : "签字笔2", "price" : 5 }
3、 和或与非等操作
$and:包含多个条件,他们之间为and的关系
MongoDB默认就是and,如:
>db.product.find({"id":"1","name":"铅笔"});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
如果需要接其他条件。则需要显示配置。语法:
db.表明.find({$and:[{条件一:条件值},{条件二:条件值},…….]});
例如:
>db.product.find({$and:[{"id":"1"},{"name":"铅笔"}]});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
$or :包含多个条件,他们之间为or的关系,$nor相当于or取反
关系为or
语法:
db.表明.find({$or:[{条件一:条件值},{条件二:条件值},…….]}); 只要满足一个条件都可以查询出来
db.表明.find({$nor:[{条件一:条件值},{条件二:条件值},…….]}); 只要满足一个条件都可以不查询出来
例如:
>db.product.find({$or:[{"id":"1"},{"name":"钢笔"}]});
{ "_id" :ObjectId("57182150560ca68a25e2991c"), "id": "1", "name" : "铅笔","price" : 4 }
{ "_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" :"钢笔", "price" :6 }
>db.product.find({$nor:[{"id":"1"},{"name":"钢笔"}]});
{"_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
$not:用作其他条件之上,取反
不能用在顶级操作符上,且必须跟正则表达式或者是文档,对象类型
错误用法1:
>db.product.find({$not:{"id":"1"}});
Error:error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg": "unknown top level operator: $not",
"code" : 2
}
错误用法2:
>db.product.find({"id":{$not:"1"}});
Error:error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg": "$not needs a regex or a document",
"code" : 2
}
正确用法是用在正则表达式或者文档对象上:
文档:
>db.product.find({"price":{$not:{$gt:5}}});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
{"_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
正则:
>db.product.find({"id":{$not:/1/}});
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
{"_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
$mod:将查询的值除以第一个给定的值取模,如果余数等于等二个值则匹配成功,
语法:> db.表明.find({取模字段:{$mod:[基数,余数]}});
如:
>db.product.find({"price":{$mod:[3,0]}});
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔", "price" : 6 }
>db.product.find({"price":{$mod:[3,1]}});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔", "price" : 4 }
$in:查询一个键的多个值,只要键匹配其中一个即可 , $nin为不包含
语法:
db.表明.find({字段:{$in:[值1,值2,…….]}});只要字段满足其中一个值都查询出来
db.表明.find({字段:{$nin:[值1,值2,…….]}});只要字段满足其中一个值都不查询出来
如:
>db.product.find({"price":{$in:[5,6]}});
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
{"_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2", "price": 5 }
>db.product.find({"price":{$nin:[5,6]}});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
$all:键需要匹配所有的值,
语法db.表明.find({字段:{$all : [值1,值2,………]}});字段必须同时包含拟定的所有值才能匹配;
主要用在数据里面,数组中必须包含所有值,
如:
>db.product.find({"socp":{$all:[1,2,5]}});
>
>db.product.find({"socp":{$all:[1,2]}});
{"_id" : ObjectId("571979394cdf1f6d55d21e99"),"id" : "4", "name" : "签字笔2","price" : 5, "socp" : [ 1, 2, 3 ] }
$exists:检查某个键是否存在,1表示存在,0表示不存在
语法:
db.表名.find({字段名:{$exists: n }}); 如果n为1,表示字段必须存在,如果为0,表示没有该字段
如
>db.product.find({socp:{$exists:1}});
{"_id" : ObjectId("571979394cdf1f6d55d21e99"),"id" : "4", "name" : "签字笔2","price" : 5, "socp" : [ 1, 2, 3 ] }
>db.product.find({socp:{$exists:0}});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
{"_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
null类型:不仅能匹配键的值为null,还匹配键不存在的情况
语法:db.表名.find({字段名:null});,匹配没有该字段或者字段值为null的数据,如:
>db.product.find();
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : null, "price" : 4}
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "price" : 6 }
{"_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
{"_id" : ObjectId("571979394cdf1f6d55d21e99"),"id" : "4", "name" : "签字笔2","price" : 5, "socp" : [ 1, 2, 3 ] }
>db.product.find({"name":null});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : null, "price" : 4}
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "price" : 6 }
综合:查询name存在,且name值为null的:
>db.product.find({"name":{$in:[null],$exists:1}});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : null, "price" : 4}
4、 mongodb兼容正则表达式查询:
>db.product.find({"name":/笔/});
{ "_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
{ "_id" : ObjectId("571979394cdf1f6d55d21e99"),"id" : "4", "name" : "签字笔2","price" : 5, "socp" : [ 1, 2, 3 ] }
5、 对数组元素的查询,或集合元素的查询
> db.product.find();
{ "_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : null, "price" : 4}
{ "_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "price" : 6 }
{ "_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
{ "_id" : ObjectId("571979394cdf1f6d55d21e99"),"id" : "4", "name" : "签字笔2","price" : 5, "socp" : [ 1, 2, 3 ] }
a) 动过单个元素匹配
语法 db.表明.find({数组字段:a),a为匹配的元素。如果数组字段的值包含 a则满足条件,如:
Db.product.find({"socp":1});
{ "_id" :ObjectId("571979394cdf1f6d55d21e99"), "id" : "4","name" : "签字笔2", "price" : 5, "socp" : [ 1, 2, 3 ] }
b) 多个元素匹配,元素的顺序无所谓
语法 db.表明.find({数组字段:{$all:[a,b]}}),a,b为匹配的元素。如果数组字段的值同时包含 a,b则满足条件,a,b的顺序无要求,如:
>db.product.find({socp:{$all:[1,2]}});
{ "_id" :ObjectId("571979394cdf1f6d55d21e99"), "id" : "4","name" : "签字笔2", "price" : 5, "socp" : [ 1, 2, 3 ] }
c) 可以使用索引指定查询数组特定位置,索引从0开始;
语法 db.表明.find({“数组字段.索引号”:value}),,value为对应索引应该配置的值,如果数组在索引号位置的值和value相等。则满足条件
>db.product.find({"socp.1":2});
{"_id" : ObjectId("571979394cdf1f6d55d21e99"),"id" : "4", "name" : "签字笔2","price" : 5, "socp" : [ 1, 2, 3 ] }
d) 查询某个长度的数组,使用$size
> db.product.find({socp:{$size:3}});
{ "_id" :ObjectId("571979394cdf1f6d55d21e99"), "id" : "4","name" : "签字笔2", "price" : 5, "socp" : [ 1, 2, 3 ] }
e) 使用$slice,正数是前面多少条,负数是尾部多少条,也可以指定偏移量和要返回的元素数量
>db.product.find({"id":"4"},{"socp":{$slice:2}});
{ "_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
{ "_id" :ObjectId("571979394cdf1f6d55d21e99"), "id" : "4","name" : "签字笔2", "price" : 5,"socp" : [ 1, 2 ] }
偏移量(偏移2位)
>db.product.find({"id":"4"},{"socp":{$slice:[2,1]}});
{ "_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔2","price" : 5 }
{ "_id" :ObjectId("571979394cdf1f6d55d21e99"), "id" : "4","name" : "签字笔2", "price" : 5, "socp" : [ 3 ] }
f) 可以使用$来指定符合条件的任意一个数组元素,如:{”users.$”:1}
> db.product.find({"socp":{$in:[1,2]}},{"socp.$":1});
{ "_id" :ObjectId("571979394cdf1f6d55d21e99"), "socp" : [ 1 ] }
g) $elemMatch:要求同时使用多个条件语句来对一个数组元素进行比较判断
我们要查询数组中每个元素满足大于2小于4的数据
> db.product.find({"socp":{$gt:2,$lt:2}});
{ "_id" :ObjectId("571979394cdf1f6d55d21e99"), "id" : "4","name" : "签字笔2", "price" : 5, "socp" : [ 1, 2, 3 ] }
这个语法在非数组情况下可以使用,用到数组后则不满足条件了,会匹配出数组中有至少有一个元素必须大于2,小于2的都满足,如果要完全匹配则用$elemMatch使用
> db.product.find({"socp":{$elemMatch:{$gt:2,$lt:2}}});
>
>db.product.find({"socp":{$elemMatch:{$gt:0,$lt:4}}});
{ "_id" :ObjectId("571979394cdf1f6d55d21e99"), "id" : "4","name" : "签字笔2", "price" : 5, "socp" : [ 1, 2, 3 ] }
6、 查询内嵌文档
a) 查询子文档
b) 语法:db.表明.find({“内嵌文档字段名.内嵌文档属性名”:条件});
>db.product.find({"socp.child":3});
{ "_id" : ObjectId("5729563ad4e95f820161c16c"),"id" : "2", "name" : "粉笔", "socp" : {"child" : 3 } }
c) $where语法,通过JavaScript,通过编程来解决查询的复杂的匹配。。但是性能很差。不建议使用
> function nameNotEntity(){
for(var doc in this){
return true;
}
}
>db.product.find({$where:nameNotEntity});
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : null, "price" : 4}
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "price" : 6 }
{"_id" : ObjectId("571979394cdf1f6d55d21e99"),"id" : "4", "name" : "签字笔2", "price" : 5,"socp" : [ 1, 2, 3 ] }
{"_id" : ObjectId("5729563ad4e95f820161c16c"),"id" : "2", "name" : "粉笔", "socp" : {"child" : 3 } }
-
顶
- 0
-
踩