mongodb-查询
基本 语法: db.collection.find( <query filter>, <projection> )
query filter
过滤条件projection
又返回的字段,默认全部返回
可以对cursor
进行修饰,添加范围limits
,跳过skips
和排序sort
限制。如果未进行sort()
,返回集合是没有排序的。
插入数据:
db.users.insertMany( [ { _id: 1, name: "sue", age: 19, type: 1, status: "P", favorites: { artist: "Picasso", food: "pizza" }, finished: [ 17, 3 ], badges: [ "blue", "black" ], points: [ { points: 85, bonus: 20 }, { points: 85, bonus: 10 } ] }, { _id: 2, name: "bob", age: 42, type: 1, status: "A", favorites: { artist: "Miro", food: "meringue" }, finished: [ 11, 25 ], badges: [ "green" ], points: [ { points: 85, bonus: 20 }, { points: 64, bonus: 12 } ] }, { _id: 3, name: "ahn", age: 22, type: 2, status: "A", favorites: { artist: "Cassatt", food: "cake" }, finished: [ 6 ], badges: [ "blue", "red" ], points: [ { points: 81, bonus: 8 }, { points: 55, bonus: 20 } ] }, { _id: 4, name: "xi", age: 34, type: 2, status: "D", favorites: { artist: "Chagall", food: "chocolate" }, finished: [ 5, 11 ], badges: [ "red", "black" ], points: [ { points: 53, bonus: 15 }, { points: 51, bonus: 15 } ] }, { _id: 5, name: "xyz", age: 23, type: 2, status: "D", favorites: { artist: "Noguchi", food: "nougat" }, finished: [ 14, 6 ], badges: [ "orange" ], points: [ { points: 71, bonus: 20 } ] }, { _id: 6, name: "abc", age: 43, type: 1, status: "A", favorites: { food: "pizza", artist: "Picasso" }, finished: [ 18, 12 ], badges: [ "black", "blue" ], points: [ { points: 78, bonus: 8 }, { points: 57, bonus: 7 } ] } ] );
Select All Documents in a Collection 查询所有集合
db.users.find( {} )
/*这个两个是一样的*/
db.users.find()
Specify Equality Condition eq
等于条件
使用键值对<field>:<value>
来表示 等于查询条件,会查出集合内<field>
等于<value>
的所有记录。
db.users.find( { status: "A" } )
Specify Conditions Using Query Operators 指定条件的查询操作
查询条件可以用下面的格式设置查询条件:
{ <field1>: { <operator1>: <value1> }, ... }
下面的示例检索user
集合,查询其中status
等于"P"
或"D"
的所有文件:
db.users.find( { status: { $in: [ "P", "D" ] } } ) //in条件查询
Specify AND Conditions AND
条件
复合查询可以指定集合的多个字段条件,AND
条件会在复合查询时隐式调用,下面的示例表示查询users
集合中status
等于"A"
并且age
小于30
的所有文档。
db.users.find( { status: "A", age: { $lt: 30 } } )//$lt less than 小于
Specify OR Conditions OR
条件
使用$or
关键字 ,可以从集合在至少满足一个$or
连接的条件的文档。
下面的示例,检索出的 status
等于 "A"
或age
小于 30
的所有文档
db.users.find({$or:[ { status: "A" }, { age:{ $lt: 30 } } ] } )
Specify AND as well as OR Conditions 使用OR
和AND
复合查询
db.users.find({status:"A", $or: [ { age: { $lt: 30 } }, { type:1 } ] } )
Exact Match on the Embedded Document 完全匹配
{<field>:<value>}
必须要完全匹配,包括field
顺序。
db.users.find( { favorites:{ artist: "Picasso", food: "pizza" } } )
- 1
Equality Match on Fields within an Embedded Document 在嵌入对象匹配字段
使用dot notation
点符号匹配嵌入对象的指定字段。等于查询 (eq)嵌入对象的特定字段,会查询集合中所有含有指定字段和指定值的嵌入对象。嵌入对象可以含有不在查询范围内的其他字段
db.users.find( { "favorites.artist": "Picasso" } )
Query on Arrays 查询数组
当字段包含一个数组,可以查询一个确切的阵列匹配或数组中的特定值。如果数组包含嵌入的文档,你可以使用点符号查询嵌入文件的特定字段。
如果使用$elemMatch
关键字指定多个条件,数组必须包含满足所有条件的至少一个元素。See Single Element Satisfies the Criteria.
如果指定了多个条件,不使用$elemMatch
关键字,必须满足所有条件。 See Combination of Elements Satisfies the Criteria
Exact Match on an Array 完全匹配
db.users.find( { badges: [ "blue", "black" ] } )
//查询元素完全匹配(包括顺序)的文档.
//查询badges数组内容为[ "blue", "black" ]的记录
Match an Array Element 查询数组内含有该元素
等于查询可以查询数组中的单个元素,会查出指定字段至少含有一个该元素的文档
db.users.find( { badges: "black" } )
//查询badges数组内含有为"black"的记录
- 1
- 2
Match a Specific Element of an Array 查询数组指定位置的一个元素
- Single Element Satisfies the Criteria
等于查询可以使用点符号查询数组指定索引或者位置的元素
db.users.find( { "badges.0": "black" } )
//查询badges数组第0个元素为"black"的记录
- 1
- 2
- Specify Multiple Criteria for Array Elements 至少有一个满足查询条件 (AND)
使用$elemMatch
关键字指定多个查询条件对数组进行查询。
db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } ) //查询finished数组内一个元素满足 大于15 AND 小于20
- 1
- 2
- Combination of Elements Satisfies the Criteria 满足查询条件的数组,
db.users.find( { finished: { $gt: 15, $lt: 20 } } )
//查询数组内所有元素满足 大于15,小于20 //[25,11] 这个数组内 有元素大于15,有元素小于15。满足查询条件 //[18,1] 满足
- 1
- 2
- 3
- 4
Array of Embedded Documents 嵌套对象数组
- Match a Field in the Embedded Document Using the Array Index 使用数组索引指定嵌入对象的字段
如果你指定要查询嵌入对象在数组中的索引,你可以使用点符号
指定文档
db.users.find( { 'points.0.points': { $lte: 55 } } )
//查询points数组中第一个元素的points字段小于或等于55
- 1
- 2
- Match a Field Without Specifying Array Index 查询一个字段,没有索引
如果不知道数组中的文档的索引位置,使用数组名
+点符号
+字段名
+查询条件
进行查询
db.users.find( { 'points.points': { $lte: 55 } } )
//查询points数组中所有元素的points对象小于或等于55
Specify Multiple Criteria for Array of Documents 多条件查询对象数组
- Single Element Satisfies the Criteria 单元素满足 (
$elemMatch
)
使用$elemMatch
关键字 查询数组内至少一个元素的同时满足所有指定字段的条件
db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } )
//查询`points`数组中至少有个元素 points小于等于70 bonus等于20 的记录
- 1
- 2
- Combination of Elements Satisfies the Criteria 任意元素满足条件
db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } ) //查询 数组内 任意元素 满足 points小于等于70 和 bonus等于20
- 1
- 2
Additional Methods 其他方法
db.collection.findOne()
等同于db.collection.find().limit(1)
转载于:https://www.cnblogs.com/anxbb/p/9351084.html
mongodb-查询相关推荐
- mongodb不等于某个值_MongoDb进阶实践之四 MongoDB查询命令详述
一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命名和删除.有了这些基 ...
- MongoDB查询(上)
MongoDB查询(上) 1.find MongoDB使用find来进行查询.查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.find的第一个参数 决定了要返回哪些文档.其形式也是 ...
- mongodb 查询效率_2020年9个好用的MongoDB 图形化界面工具
市场上有许多MongoDB管理工具.这些工具可以提高MongoDB开发和管理的效率.下面我们就列举一些2020年好用的mongo管理工具. 1)NoSQLBooster 用于MongoDB的NoSQL ...
- Mongodb查询语句与Sql语句对比
左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * from users db.users.find({"age" ...
- Mongodb查询分析器解析
Mongodb查询分析器 动态相关项目中涉及到数据量大和吞吐量的接口,例如关注页面动态,附近动态,这部分数据都是存储在mongodb中,在线上数据中分类两个mongodb集合存储 其中关注动态基于扩散 ...
- MongoDB查询实现 笛卡尔积,Union All 和Union 功能
转载自 MongoDB查询实现 笛卡尔积,Union All 和Union 功能 此篇文章及以后的文章大部分都是从聚合管道(aggregation pipeline)的一些语法为基础讲解的,如果不 ...
- mongodb查询内嵌文档
mongodb查询内嵌文档 假设有这样一个文档: db.XXX.remove(); db.XXX.insert({"id":1, "members":[{&qu ...
- mongodb查询分页优化
mongodb查询分页优化 转自:https://blog.csdn.net/chunqiuwei/article/details/11669885 项目中需要用mongodb来进行数据测存储和查询, ...
- mongodb 查询内嵌文档
原文:http://www.cnblogs.com/silentjesse/p/3598399.html mongodb查询内嵌文档 假设有这样一个文档: db.XXX.remove(); db.XX ...
- 跨mysql和mongodb查询工具_MySQL与MongoDB查询互转
Mysql与MongoDB查询互转 mongo查询严格要求数据格式! 1.只想查出某些数据,不想全部数据都查出来 mysql:select name from user; mongo: db.user ...
最新文章
- 【对讲机的那点事】关于对讲机锂电池你了解多少?
- 一个爬虫的故事:这是人干的事儿?
- docker设置固定ip地址
- redisson redlock(基于redisson框架和redis集群使用分布式锁)
- OpenCL-3-同步机制
- [转] Windows CE 6.0 启动过程分析
- 详述白盒测试逻辑覆盖法的语句覆盖及其缺点
- 扫盲了,RC,RTM,Beta等各个版本的意思
- Hyperledger Fabric 实战(十): Fabric node SDK 样例 - 投票DAPP
- 知识星球限时优惠活动,速进!
- 定时自动关机计划命令
- MyBatis文档观后整理
- python zip 压缩文件夹
- PacBio和NanoPore两种三代测序仪的比较
- libjpeg库使用举例
- 深度链接 Deep Link 开发遇到的坑
- 音视频入门-10-使用libyuv对YUV数据进行缩放、旋转、镜像、裁剪、混合
- Rosetta基础(3)--Rosetta能量函数简介
- android wear hce,最全谷歌Android Wear 2.0设备升级名单:华为威武
- PTA 7-5 最大公约数和最小公倍数 (10 分)