MongoDB常用命令汇总之聚集集合查询。
2017-07-06 20:41
399 查看
查询所有记录
db.userInfo.find(); // 相当于 select * from userInfo
默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。
注意:键入it命令不能带“;”。
但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize = 50;这样每页就显示50记录了。
查询去掉后的当前聚集集合中的某列的重复数据
db.userInfo.distinct("name"); // 会过滤掉name中的相同数据
相当于:select distinct name from userInfo;
查询age = 22的记录
db.userInfo.find({"age":22});
相当于:select * from userInfo Where age = 22;
查询age > 22的记录
db.userInfo.find({"age":{$gt:22}});
相当于select * from userInfo where age > 22;
查询age < 22 的记录
db.userInfo.find({age : {$lt : 22}});
相当于select * from userInfo where age < 22;
查询 age >= 25的记录
db.userInfo.find(age : {$gte : 25});
相当于 select * from userInfo where age >= 25;
查询 age <= 25 的记录
db.userInfo.find(age : {$lte : 25});
相当于 select * from userInfo where age <= 25;
查询 age >= 23 并且 age <= 26
db.userInfo.find({age : {$gte : 23, $lte:26}});
相当于 select * from userInfo where age >= 23 and age <=26;
查询name中包含mongo的数据
db.userInfo.find({ name : /mongo/});
相当于%% select * from userInfo where name like '%mongo%'
查询name中以mongo开头的
db.userInfo.find("name : /^mongo/");
相当于select * from userInfo where name like 'mongo%';
查询指定列name、age数据
db.userinfo.find({}, {name:1, age:1});
相当于:select name,age from userInfo;
当然name也可以用true或false,当用true的情况下和name:1的效果一样,如果用false就是排除name,显示name以外的列信息。
查询指定列name、age数据,age > 25
db.userInfo.find({age : {$gt : 25}}, {name : 1 , age : 1})
相当于: select name, age from userInfo where age > 25;
按照年龄排序:
升序:db.userInfo.find().sort({age : 1});
降序:db.userInfo.find().sort({age : -1});
查询name = zhangsan,age = 22的数据
db.userInfo.find({name : 'zhangsan' , age : 22});
相当于:select * from userInfo where name = 'zhangsan' and age = 22;
查询前5条记录
db.userInfo.find().limit(5);
相当于:select * from userInfo limit 5;
查询10条以后的数据
db.userinfo.find().skip(10);
查询在5-10之间的数据
db.userInfo.find().limit(10).skip(5);
可用于分页,limit是pageSize,skip是第几页*pageSize
or与查询
db.userInfo.find({$or : [{age : 22}, {age : 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
查询第一条数据
db.userInfo.findOne();
相当于:select * from userInfo limit 1;
db.userInfo.find().limit(1);
查询某个结果集的记录条数
db.userInfo.find({age : {$gte : 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
按照某列进行排序
db.userInfo.find({sex : {$exists : true}}).count();
相当于:select count(sex) from userInfo;
db.userInfo.find(); // 相当于 select * from userInfo
默认每页显示20条记录,当显示不下的情况下,可以用it迭代命令查询下一页数据。
注意:键入it命令不能带“;”。
但是你可以设置每页显示数据的大小,用DBQuery.shellBatchSize = 50;这样每页就显示50记录了。
查询去掉后的当前聚集集合中的某列的重复数据
db.userInfo.distinct("name"); // 会过滤掉name中的相同数据
相当于:select distinct name from userInfo;
查询age = 22的记录
db.userInfo.find({"age":22});
相当于:select * from userInfo Where age = 22;
查询age > 22的记录
db.userInfo.find({"age":{$gt:22}});
相当于select * from userInfo where age > 22;
查询age < 22 的记录
db.userInfo.find({age : {$lt : 22}});
相当于select * from userInfo where age < 22;
查询 age >= 25的记录
db.userInfo.find(age : {$gte : 25});
相当于 select * from userInfo where age >= 25;
查询 age <= 25 的记录
db.userInfo.find(age : {$lte : 25});
相当于 select * from userInfo where age <= 25;
查询 age >= 23 并且 age <= 26
db.userInfo.find({age : {$gte : 23, $lte:26}});
相当于 select * from userInfo where age >= 23 and age <=26;
查询name中包含mongo的数据
db.userInfo.find({ name : /mongo/});
相当于%% select * from userInfo where name like '%mongo%'
查询name中以mongo开头的
db.userInfo.find("name : /^mongo/");
相当于select * from userInfo where name like 'mongo%';
查询指定列name、age数据
db.userinfo.find({}, {name:1, age:1});
相当于:select name,age from userInfo;
当然name也可以用true或false,当用true的情况下和name:1的效果一样,如果用false就是排除name,显示name以外的列信息。
查询指定列name、age数据,age > 25
db.userInfo.find({age : {$gt : 25}}, {name : 1 , age : 1})
相当于: select name, age from userInfo where age > 25;
按照年龄排序:
升序:db.userInfo.find().sort({age : 1});
降序:db.userInfo.find().sort({age : -1});
查询name = zhangsan,age = 22的数据
db.userInfo.find({name : 'zhangsan' , age : 22});
相当于:select * from userInfo where name = 'zhangsan' and age = 22;
查询前5条记录
db.userInfo.find().limit(5);
相当于:select * from userInfo limit 5;
查询10条以后的数据
db.userinfo.find().skip(10);
查询在5-10之间的数据
db.userInfo.find().limit(10).skip(5);
可用于分页,limit是pageSize,skip是第几页*pageSize
or与查询
db.userInfo.find({$or : [{age : 22}, {age : 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
查询第一条数据
db.userInfo.findOne();
相当于:select * from userInfo limit 1;
db.userInfo.find().limit(1);
查询某个结果集的记录条数
db.userInfo.find({age : {$gte : 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
按照某列进行排序
db.userInfo.find({sex : {$exists : true}}).count();
相当于:select count(sex) from userInfo;
相关文章推荐
- MongoDB常用命令汇总之Collection聚集集合和用户相关。
- MongoDB常用命令汇总之查看聚集集合基本信息。
- MongoDB的一些基本操作命令(二)————聚集集合查询和索引
- MongoDB常用命令汇总之修改、添加、删除集合数据。
- 【Mongodb教程 第十八课 】MongoDB常用命令 数据库命令 集合操作命令
- Mongodb常用命令之查询
- MongoDB常用命令汇总之条件操作符。
- MongoDB常用命令汇总之语句块操作。
- MongoDB创建表步骤,Mongo常用的数据库操作命令,查询,添加,更新,删除_MongoDB 性能监测
- MongoDB添加用户,登录命令,查询所有数据库集合命令
- MongoDB常用命令汇总之插入、更新和删除操作详解。
- mongodb常用维护命令汇总
- MongoDB创建表步骤,Mongo常用的数据库操作命令,查询,添加,更新,删除_MongoDB 性能监测
- MongoDB常用命令查询基础类
- MongoDB常用命令汇总之数据库常用命令。
- MongoDB实战全攻略(含安装常用命令备份恢复及主从(含副本集合)部署)
- mongoDB集合 文档创建修改删除以及查询命令总结
- mongodb常用的查询命令例子
- Cordova - 常用的插件汇总(附插件的安装、查询、更新、删除等命令)
- MongoDB常用命令汇总之基本聚合函数。