mongodb常用查询语句
1、查询所有记录
db.userInfo.find();
相当于:select* from userInfo;
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、按照年龄排序 1 升序 -1 降序
升序: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);
相当于:selecttop 5 * from userInfo;
16、查询 10 条以后的数据
db.userInfo.find().skip(10);
相当于:select * from userInfo where id not in (
selecttop 10 * from userInfo
);
17、查询在 5-10 之间的数据
db.userInfo.find().limit(10).skip(5);
可用于分页,limit 是 pageSize,skip 是第几页*pageSize
18、or 与 查询
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相当于:select * from userInfo where age = 22 or age = 25;
19、findOne 查询第一条数据
db.userInfo.findOne();
相当于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);
20、查询某个结果集的记录条数 统计数量
db.userInfo.find({age: {$gte: 25}}).count();
相当于:select count(*) from userInfo where age >= 20;
如果要返回限制之后的记录数量,要使用 count(true)或者 count(非 0)
db.users.find().skip(10).limit(5).count(true);
mongodb常用查询语句的更多相关文章
- mongodb常用查询语句(转)
1.查询所有记录 db.userInfo.find();相当于:select* from userInfo; 2.查询去掉后的当前聚集集合中的某列的重复数据db.userInfo.distinct(& ...
- mongodb常用操作语句
mongodb常用操作语句 A:创建数据表 db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean&g ...
- 23个MySQL常用查询语句
23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...
- Hibernate常用查询语句
Hibernate常用查询语句 Hib的检索方式1'导航对象图检索方式.通过已经加载的对象,调用.iterator()方法可以得到order对象如果是首次执行此方法,Hib会从数据库加载关联的orde ...
- mongodb的查询语句学习摘要
看了些资料,对应只需要知道怎么查询和使用mongodb的我来说,这些足够啦. 左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * fr ...
- MySQL常用查询语句汇总(不定时更新)
在这篇文章中我会通过一些例子来介绍日常编程中常用的SQL语句 目录: ## 1.数据库的建立 ## 1.数据库的建立 实例将ER图的形式给出: 由此转换的4个关系模式: ...
- mysql—常用查询语句总结
关于MySQL常用的查询语句 一查询数值型数据: ; 查询谓词:>,=,<,<>,!=,!>,!<,=>,=< 二查询字符串 SELECT * FROM ...
- MongoDB常用查询,排序,group,SpringDataMongoDB update group
MongoDB查询 指定查询并排序 db.getCollection('location').find({"site.id":"川A12345","s ...
- MongoDB简单查询语句<平时使用语录,持续更新>
MongoDB查询语句 --查询近三个月的客户使用量 aggregate:使用聚合 match:过滤 group分组 -- mysql中select org_code as 近三个月使用商户 ...
随机推荐
- spark-sql缩减版样例:获取每日top3搜索词和各自的次数,包括总次数
//获取出每天前3的搜索词 ArrayList<String> log = new ArrayList<String>(); log.add("2015-10-01, ...
- RoR - Action Pack
什么是Action Pack: Rest and Rails: Rest if all about resources rake routes: print out all routes
- 【Python全栈-CSS】CSS实现网页背景图片自适应全屏
CSS实现网页背景图片自适应全屏 功能:实现能自适应屏幕大小又不会变形的背景大图,而且背景图片不会随着滚动条滚动而滚动. 以下是用CSS实现的方法: <html> <head> ...
- C# .net 语言加密方案
C# .net 语言加密方案 方案背景 当前C# .net语言的应用范围越来越广泛,IIS 的服务器架构后台代码.桌面应用程序的 winform .Unity3d 的逻辑脚本都在使用.C# .net ...
- 动态代理处理service
/* 动态代理处理service * 1.动态代理的核心是切面编程,去除重复代码: * 2.通过反射+注解可以灵活的获取传入对象内容: * 3.通过try+catch将多个操作包裹,实现事物的原子性: ...
- jackson 流式API
http://www.cnblogs.com/lee0oo0/articles/2652528.html Jackson提供了三种可选的JSON处理方法 1.流式API com.fasterx ...
- Js数组去重方法总结
//方法一 var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5]; function removeDuplicatedItem(arr) { for(var i = 0; ...
- “行业客户云原生最佳实践日” 亮相KubeCon上海
2018年11月13日至15日,由CNCF主办的KubeCon + CloudNativeCon将首次登陆中国上海,这是全球范围内规模最大的Kubernetes和云原生技术盛会. 唯一聚焦客户实践的分 ...
- C++ cout格式化输出
表1:C++ 流操纵算子 流操纵算子 作 用 *dec 以十进制形式输出整数 常用 hex 以十六进制形式输出整数 oct 以八进制形式输出整数 fixed 以普通小数形式输出浮点数 scienti ...
- 极致21点开发DAY4
完成的内容:1.修改上一篇博文中的Bug 2.完成任务窗口逻辑 using System; using System.Collections.Generic; using UnityEngine; ...