MongoDB查询命令具体解释
1、查询全部记录
复制代码代码例如以下:
db.userInfo.find();
相当于:select* from userInfo;
默认每页显示20条记录,当显示不下的情况下,能够用it迭代命令查询下一页数据。注意:键入it命令不能带“;”
可是你能够设置每页显示数据的大小。用DBQuery.shellBatchSize= 50;这样每页就显示50条记录了。
2、查询去掉后的当前聚集集合中的某列的反复数据
复制代码代码例如以下:
db.userInfo.distinct("name");
会过滤掉name中的同样数据
相当于:select distict name from userInfo;
3、查询age = 22的记录
复制代码代码例如以下:
db.userInfo.find({"age": 22});
相当于: select * from userInfo where age = 22;
4、查询age > 22的记录
复制代码代码例如以下:
db.userInfo.find({age: {$gt: 22}});
相当于:select * from userInfo where age >22;
5、查询age < 22的记录
复制代码代码例如以下:
db.userInfo.find({age: {$lt: 22}});
相当于:select * from userInfo where age <22;
6、查询age >= 25的记录
复制代码代码例如以下:
db.userInfo.find({age: {$gte: 25}});
相当于:select * from userInfo where age >= 25;
7、查询age <= 25的记录
复制代码代码例如以下:
db.userInfo.find({age: {$lte: 25}});
8、查询age >= 23 而且 age <= 26
复制代码代码例如以下:
db.userInfo.find({age: {$gte: 23, $lte: 26}});
9、查询name中包括 mongo的数据
复制代码代码例如以下:
db.userInfo.find({name: /mongo/});
//相当于%%
select * from userInfo where name like ‘%mongo%';
10、查询name中以mongo开头的
复制代码代码例如以下:
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%';
11、查询指定列name、age数据
复制代码代码例如以下:
db.userInfo.find({}, {name: 1, age: 1});
相当于:select name, age from userInfo;
当然name也能够用true或false,当用ture的情况下河name:1效果一样。假设用false就是排除name。显示name以外的列信息。
12、查询指定列name、age数据, age > 25
复制代码代码例如以下:
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于:select name, age from userInfo where age >25;
13、依照年龄排序
复制代码代码例如以下:
升序:
db.userInfo.find().sort({age: 1});
降序:
db.userInfo.find().sort({age: -1});
14、查询name = zhangsan, age = 22的数据
复制代码代码例如以下:
db.userInfo.find({name: 'zhangsan', age: 22});
相当于:select * from userInfo where name = ‘zhangsan' and age = ‘22';
15、查询前5条数据
复制代码代码例如以下:
db.userInfo.find().limit(5);
相当于:select top 5 * from userInfo;
16、查询10条以后的数据
复制代码代码例如以下:
db.userInfo.find().skip(10);
相当于:
select * from userInfo where id not in (
select top 10 * from userInfo
);
17、查询在5-10之间的数据
复制代码代码例如以下:
db.userInfo.find().limit(10).skip(5);
limit是限制显示的数量,skip是跳过的数量。
18、or与 查询
复制代码代码例如以下:
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
19、查询第一条数据
复制代码代码例如以下:
db.userInfo.findOne();
相当于:
select top 1 * from userInfo;
db.userInfo.find().limit(1);
20、查询某个结果集的记录条数
复制代码代码例如以下:
db.userInfo.find({age: {$gte: 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
21、依照某列进行排序
复制代码代码例如以下:
db.userInfo.find({sex: {$exists: true}}).count();
相当于:select count(sex) from userInfo;
MongoDB查询命令具体解释的更多相关文章
- MongoDb进阶实践之六 MongoDB查询命令详述(补充)
一.引言 上一篇文章我们已经介绍了MongoDB数据库的查询操作,但是并没有介绍全,随着自己的学习的深入,对查询又有了新的东西,决定补充进来.如果大家想看上一篇有关MongoDB查询的 ...
- MongoDb进阶实践之三 MongoDB查询命令详述
一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命 ...
- MongoDb进阶实践之四 MongoDB查询命令详述
一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命名和删除.有了这些基 ...
- Mongodb查询命令详解
前面我们简单的讲了下find方法,下面来深入的过一下它的用法以及常用的字方法. 下面是mongo中db.user.help()中对find方法的定义和解释: db.user.find([query], ...
- MongoDB查询修改操作语句命令大全
MongoDB查询更新操作语句命令大全 查询操作 1.条件操作符 <, <=, >, >= 这个操作符就不用多解释了,最常用也是最简单的db.collection.find({ ...
- MongoDB常用命令
本文整理了一年多以来我常用的MongoDB操作,涉及mongo-shell.pymongo,既有运维层面也有应用层面,内容有浅有深,这也就是我从零到熟练的历程. MongoDB的使用之前也分享过一篇, ...
- MongoDB 基础命令行
本文专门介绍MongoDB的命令行操作.其实,这些操作在MongoDB官网提供的Quick Reference上都有,但是英文的,为了方便,这里将其稍微整理下,方便查阅. 登录和退出 mongo命令直 ...
- Mongodb查询的用法,备注防止忘记
最近在用这个东西,为防止忘记,记下来. 集合简单查询方法 mongodb语法:db.collection.find() //collection就是集合的名称,这个可以自己进行创建. 对比sql语句 ...
- Mongodb数据更新命令
一.Mongodb数据更新命令 Mongodb更新有两个命令:update.save. 1.1update命令 update命令格式: db.collection.update(criteria,ob ...
随机推荐
- ImageMagick 压缩图片 方法
Images as a percentage of page weight for the Alexa top 10 global web sites 图片在站点所占的比重越来越重.更好的优化图片能 ...
- MySQL主键添加/删除
2改动数据库和表的字符集alter database maildb default character set utf8;//改动数据库的字符集alter table mailtable defaul ...
- C# - InnerList
运行效果: 代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; name ...
- 在Windows上使用CodeLite+MinGW+Clang进行开发
前几天听说clang 3.4已经release了,然后我又手痒就折腾一下,在这里记录一下折腾的经过. 在以前就试过clang-cl+VC的开发环境,编译代码到是没发现什么大问题,有不少警告而已,不过c ...
- No resource found that matches the given name 'android:WindowTitle'
当你的androidAPI 由2.1版本更换成2.2版本时: res/vavlues/styles.xml中使用的android:WindowTitle会报以下异常, error: Error re ...
- No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb' Spring MVC
I'm learning the Spring Framework, and I'm doing the HelloWeb tutorial on tutorialspoint, and I can' ...
- 关于WEB三层架构的思考
1.MVC设计思想 MVC程序设计思想是眼下比較流行的WEB开发的模式,当中,M(model)是模型.即JavaBean,用来封装和保存数据:V(view)是视图,即JSP.用来显示内容:C(cont ...
- openfire数据库中文乱码问题
1.首先数据库的编码设置为UTF-8 2.项目的编码也要设置为UTF-8 假设数据保存到数据库的时候还有乱码 就要改动openfire配置文件 在openfire主文件夹\conf\openfire ...
- android5.0(Lollipop) BLE Central牛刀小试
转载请表明作者:http://blog.csdn.net/lansefeiyang08/article/details/46482073 昨天写了android L BLE Peripheral的简单 ...
- ImageMagick wrapper for php
https://code.google.com/archive/p/phmagick/