MongoDB 查询SQL 大全
mongodb与sql语句对比
- 左边是mongodb查询语句,右边是sql语句。对照着用,挺方便。
- db.users.find() select * from users
- db.users.find({"age" : 27}) select * from users where age = 27
- db.users.find({"username" : "joe", "age" : 27}) select * from users where "username" = "joe" and age = 27
- db.users.find({}, {"username" : 1, "email" : 1}) select username, email from users
- db.users.find({}, {"username" : 1, "_id" : 0}) // no case // 即时加上了列筛选,_id也会返回;必须显式的阻止_id返回
- db.users.find({"age" : {"$gte" : 18, "$lte" : 30}}) select * from users where age >=18 and age <= 30 // $lt(<) $lte(<=) $gt(>) $gte(>=)
- db.users.find({"username" : {"$ne" : "joe"}}) select * from users where username <> "joe"
- db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}}) select * from users where ticket_no in (725, 542, 390)
- db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}}) select * from users where ticket_no not in (725, 542, 390)
- db.users.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]}) select * form users where ticket_no = 725 or winner = true
- db.users.find({"id_num" : {"$mod" : [5, 1]}}) select * from users where (id_num mod 5) = 1
- db.users.find({"$not": {"age" : 27}}) select * from users where not (age = 27)
- db.users.find({"username" : {"$in" : [null], "$exists" : true}}) select * from users where username is null // 如果直接通过find({"username" : null})进行查询,那么连带"没有username"的纪录一并筛选出来
- db.users.find({"name" : /joey?/i}) // 正则查询,value是符合PCRE的表达式
- db.food.find({fruit : {$all : ["apple", "banana"]}}) // 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录
- db.food.find({"fruit.2" : "peach"}) // 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录
- db.food.find({"fruit" : {"$size" : 3}}) // 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用
- db.users.findOne(criteria, {"comments" : {"$slice" : 10}}) // 对数组的查询,只返回数组comments中的前十条,还可以{"$slice" : -10}, {"$slice" : [23, 10]}; 分别返回最后10条,和中间10条
- db.people.find({"name.first" : "Joe", "name.last" : "Schmoe"}) // 嵌套查询
- db.blog.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}) // 嵌套查询,仅当嵌套的元素是数组时使用,
- db.foo.find({"$where" : "this.x + this.y == 10"}) // 复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where
- db.foo.find({"$where" : "function() { return this.x + this.y == 10; }"}) // $where可以支持javascript函数作为查询条件
- db.foo.find().sort({"x" : 1}).limit(1).skip(10); // 返回第(10, 11]条,按"x"进行排序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number
- 多条件查询
db.getCollection('CollectionName').find({"userId":1},{"customerId":61});
- 根据时间戳范围查询
db.getCollection('CollectionName').find({"userId":61},{"timestamp":{"$gt":1540449300000,"$lte":1540550100000}})
- 条件查排序并分页:1.是升序, -1是降序
db.getCollection('CollectionName').find({"userId":361}).sort({"time":-1}).limit(100);
- ISOdate时间范围查询
db.getCollection('CollectionName').find({ "timestamp" : { "$gte" : ISODate("2018-04-20T00:00:00Z") , "$lt" : ISODate("2018-04-21T00:00:00Z") }});
- 使用$and多条件查询
db.getCollection('CollectionName').find( {$and:[{"userId":37761},{"domain":"time.com"},{"timestamp":{"$gt":1540483200000,"$lte":1540550100000}}]});
mongodb中对应的范围标识符:
"$lt"===================> "<" "$lte"==================> "<=" "$gt"===================> ">" "$gte"==================> ">=" "$ne"===================> "!="
MongoDB 查询SQL 大全相关推荐
- MongoDB查询用法大全
转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...
- mongodb查询大全mongo语句
mongodb查询大全mongo语句 一.前言 虽然这些语句在开发当中不会使用,因为springdataMongoDB封装的非常完美了.但是这里的语句思想和关系型数据库有些区别,所以需要拿出来详细的讲 ...
- Mongodb查询语句与Sql语句对比
左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * from users db.users.find({"age" ...
- SQL 分页查询语句大全即(查找第N到M条记录的方法)
SQL 分页查询语句大全即(查找第N到M条记录的方法) 第一种方法,我的原创方法 row=2 表示分页行数 page=1 表示页码 getnum=row*page select * from (se ...
- 从SQL过渡至MongoDB查询对照表
从SQL过渡至MongoDB查询对照表 SQL 说明 Mongo 说明 CREATE TABLE USERS (a Number, b Number) Implicit or use MongoDB: ...
- mongodb 跟踪SQL语句及慢查询收集
有个需求:跟踪mongodb的SQL语句及慢查询收集 第一步:通过mongodb自带函数可以查看在一段时间内DML语句的运行次数. 在bin目录下面运行 ./mongostat -port 端口号 ...
- orcal SQL查询语句大全集锦
orcal SQL查询语句大全集锦 一. 简单查询 简单的Transact-SQL查询只包括选择列表.FROM子句和WHERE子句.它们分别说明所查询列.查询的 表或视图.以及搜索条件等. 例 ...
- mysql的查询语句大全_sql语句(sql数据库查询语句大全)
sql语句 结构化查询语言(StructuredQueryLanguage)缩写为SQL.结构化查询语言是一种数据库查询和编程语言,用于访问数据以及查询,更新和管理关系数据库系统: 程序功能 创建数据 ...
- mongodb不等于某个值_MongoDb进阶实践之四 MongoDB查询命令详述
一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命名和删除.有了这些基 ...
最新文章
- BZOJ3473:字符串(后缀数组,主席树,二分,ST表)
- zabbix监控管理维护脚本
- jquery 判断数据是否重复
- C++:undefined reference to vtable 原因与解决办法 [转]
- Log日志分析--awstats
- 【渝粤题库】陕西师范大学200301几何学作业(高起本)
- 21.和和instance of
- leetcode python3 简单题119. Pascal's Triangle II
- java并发编程实战电子书,王者笔记!
- 网易云音乐的焦虑 暗藏在上市后的首份财报里
- 中国互联网十大骨干网有哪些?了解互联网的真实网络结构
- 实践php检测图片木马
- ubuntu 装机必备软件
- 终于试用了64位win7
- 计算机打表格图,快速填充/微图表/一秒制作打勾方框
- 运动装备什么牌子好?运动装备品牌排行榜推荐
- Destoon增加内容页的浏览历史记录
- 现流行的第三方库及名称
- Win11WSA无法启动的解决办法
- Uninformed Students: Student–Teacher Anomaly Detection with Discriminative Latent Embeddings(翻译)