Mongo高级查询
1、条件操作符
创新互联是一家专业提供罗江企业网站建设,专注与做网站、成都网站建设、H5技术、小程序制作等业务。10年已为罗江众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
(1)条件操作符
db.collection.find({ "field" : { $gt: value } } ); // 大于: field > value
db.collection.find({ "field" : { $lt: value } } ); // 小于: field < value
db.collection.find({ "field" : { $gte: value } } ); // 大于等于: field >= value
db.collection.find({ "field" : { $lte: value } } ); // 小于等于: field <= value
(2)$all匹配所有
这个操作符跟SQL 语法的in 类似,但不同的是, in 只需满足( )内的某一个值即可, 而$all 必
须满足[ ]内的所有值,例如:
db.users.find({age : {$all : [6, 8]}});
可以查询出 {name: 'David', age: 26, age: [ 6, 8, 9 ] }
但查询不出 {name: 'David', age: 26, age: [ 6, 7, 9 ] }
(3)$exists判断字段在不在
查询所有存在age 字段的记录
db.users.find({age: {$exists: true}});
查询所有不存在name 字段的记录
db.users.find({name: {$exists: false}});
(4)Null值处理
Null不仅能找到自身而且还能找到不存在某个字段的值
(5)$mod取模运算
取模6为1的
> db.user.find()
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
> db.user.find({age:{$mod:[6,1]}})
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
(5)$ne不等于
查询age 的值不等于7 的数据
> db.user.find({age:{$ne:6}})
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
> db.user.find({age:{$ne:7}})
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
(6)$in包含
查询age 的值在7,8 范围内的数据
> db.user.find({age:{$in:[7,8]}})
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
(7)min不包含
查询age 的值在7,8 范围外的数据
> db.user.find({age:{$nin:[7,8]}})
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }
(8)$size数组元素个数
匹配db.users.find({favorite_number: {$size: 3}});
不匹配db.users.find({favorite_number: {$size: 2}});
> db.user.find({favorite_number:{$size:3}})
{ "_id" : ObjectId("579eee3d8aeb5abda0d54b5e"), "name" : "David", "age" : 26, "favorite_number" : [ 6, 7, 9 ] }
> db.user.find({favorite_number:{$size:2}})
>
(9)正则表达式匹配
查询name 不以T 开头的数据
db.user.find()
{ "_id" : ObjectId("4fb5faaf6d0f9d8ea3fc91a8"), "name" : "Tony", "age" : 20 }
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
> db.user.find({name:{$not:/^T.*/}})
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
>
(10) Javascript查询和$where查询
查询a 大于3 的数据,下面的查询方法殊途同归
db.c1.find( { a : { $gt: 3 } } );
db.c1.find( { $where: "this.a > 3" } );
db.c1.find("this.a > 3");
f = function() { return this.a > 3; } db.c1.find(f);
(11)count查询记录条数
> db.user.find()
{ "_id" : ObjectId("4fb5faaf6d0f9d8ea3fc91a8"), "name" : "Tony", "age" : 20 }
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
> db.user.find().count()
2
>
(12)skip限制返回记录的起点
查询user中的第2条数据
> db.user.find()
{ "_id" : ObjectId("4fb5faaf6d0f9d8ea3fc91a8"), "name" : "Tony", "age" : 20 }
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
> db.user.find().skip(1).limit(1)
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
>
(13)sort排序
查询c1 表按age 升序排列
> db.user.find().sort({age:-1})
{ "_id" : ObjectId("4fb5faaf6d0f9d8ea3fc91a8"), "name" : "Tony", "age" : 20 }
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
> db.user.find().sort({age:1})
{ "_id" : ObjectId("4fb5fab96d0f9d8ea3fc91a9"), "name" : "Joe", "age" : 10 }
{ "_id" : ObjectId("4fb5faaf6d0f9d8ea3fc91a8"), "name" : "Tony", "age" : 20 }
>
(14)游标
for (var i=1;i<10;i++) db.user.insert( {"x" : i})
> for(var c=db.user.find();c.hasNext();){printjson(c.next());}
{ "_id" : ObjectId("579ef6198aeb5abda0d54b5f"), "x" : 1 }
{ "_id" : ObjectId("579ef6198aeb5abda0d54b60"), "x" : 2 }
{ "_id" : ObjectId("579ef6198aeb5abda0d54b61"), "x" : 3 }
{ "_id" : ObjectId("579ef6198aeb5abda0d54b62"), "x" : 4 }
{ "_id" : ObjectId("579ef6198aeb5abda0d54b63"), "x" : 5 }
{ "_id" : ObjectId("579ef6198aeb5abda0d54b64"), "x" : 6 }
{ "_id" : ObjectId("579ef6198aeb5abda0d54b65"), "x" : 7 }
{ "_id" : ObjectId("579ef6198aeb5abda0d54b66"), "x" : 8 }
{ "_id" : ObjectId("579ef6198aeb5abda0d54b67"), "x" : 9 }
>
网页题目:Mongo高级查询
链接分享:http://ybzwz.com/article/pshpjh.html