mangoDB学习笔记(二)查询
来源:互联网 发布:阿里云上小号 编辑:程序博客网 时间:2024/05/16 17:48
Query Documents
- this page
- Query Method 查询方法
- Example Collection 集合实例
- Select All Documents in a Collection 查询所有
- Specify Query Filter Conditions 按查询条件
- Query on Embedded Documents 嵌入对象查询
- Query on Arrays 数组查询
Query Method 查询方法
MongoDB提供db.collection.find()来查询集合内的文档。返回匹配文档的游标
db.collection.find( <query filter>, <projection> )
query filter
过滤条件projection
又返回的字段,默认全部返回
可以对cursor
进行修饰,添加范围limits
,跳过skips
和排序sort
限制。如果未进行sort()
,返回集合是没有排序的。
集合实例
用于使用db.collection.find()
查询的集合。
在
shell
(命令行) 模式中,若未对cursor
使用var
关键字,返回数据只有前20个数据。
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 Query Filter Conditions 按条件查询
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条件查询
也可以使用$or
操作符执行这个查询,当在filed
上执行限制的时候用$in
比$or
更好。
查询和投影操作符完整文档
Specify AND Conditions AND
条件
复合查询可以指定集合的多个字段条件,AND
条件会在复合查询时隐式调用,下面的示例表示查询users
集合中status
等于"A"
并且age
小于30
的所有文档。
db.users.find( { status: "A", age: { $lt: 30 } } )//$lt less than 小于
SQL
SELECT * FROM users WHERE status='A' AND age<30;
Specify OR Conditions OR
条件
使用$or
关键字 ,可以从集合在至少满足一个$or
连接的条件的文档。
下面的示例,检索出的 status
等于 "A"
或age
小于 30
的所有文档
db.users.find( { $or: [ { status: "A" }, { age: { $lt: 30 } } ] })
SELECT * FROM users WHERE status='A' OR age<30;
NOTE
Queries which use comparison operators are subject to Type Bracketing.
查询所使用的比较运算符都是Type Bracketing
Specify AND as well as OR Conditions 使用OR
和AND
复合查询
db.users.find( { status: "A", $or: [ { age: { $lt: 30 } }, { type: 1 } ] })
SELECT * FROM users WHERE status='A' AND (age<30 OR type=1);
Query on Embedded Documents 嵌入式文件查询??
When the field holds an embedded document, a query can either specify an exact match on the embedded document or specify a match by individual fields in the embedded document using the dot notation.
当要查询的字段是一个嵌入对象时,查询条件可以精确匹配这个对象也可以通过 dot notation
匹配个别字段
Exact Match on the Embedded Document 完全匹配
{<field>:<value>}
必须要完全匹配,包括field
顺序。
db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } )
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"的记录
Match a Specific Element of an Array 查询数组指定位置的一个元素
- Single Element Satisfies the Criteria
等于查询可以使用点符号查询数组指定索引或者位置的元素
db.users.find( { "badges.0": "black" } )//查询badges数组第0个元素为"black"的记录
- Specify Multiple Criteria for Array Elements 至少有一个满足查询条件 (AND)
使用$elemMatch
关键字指定多个查询条件对数组进行查询。
db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )//查询finished数组内一个元素满足 大于15 AND 小于20
- Combination of Elements Satisfies the Criteria 满足查询条件的数组,
db.users.find( { finished: { $gt: 15, $lt: 20 } } )//查询数组内所有元素满足 大于15,小于20//[25,11] 这个数组内 有元素大于15,有元素小于15。满足查询条件//[18,1] 满足
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
- 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 的记录
- Combination of Elements Satisfies the Criteria 任意元素满足条件
db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } )//查询 数组内 任意元素 满足 points小于等于70 和 bonus等于20
Additional Methods 其他方法
db.collection.findOne()
等同于db.collection.find().limit(1)
- mangoDB学习笔记(二)查询
- 学习mangodb笔记1
- MangoDb学习笔记一
- MongoDB学习笔记(二)(查询)
- MongoDB 学习笔记(二) 之查询
- mongoDB 学习笔记(二)聚合查询
- 学习mangodb的资料
- MangoDB在java中的查询
- Oracle学习笔记(四):多表查询(二)
- 数据库学习笔记(二)---SQL查询语句
- Wireshark使用方法(学习笔记二——查询统计)
- Wireshark使用方法(学习笔记二——查询统计)
- Wireshark使用方法(学习笔记二——查询统计)
- Wireshark使用方法(学习笔记二——查询统计)
- Wireshark使用方法(学习笔记二——查询统计)
- LINQ之查询基础学习笔记(二)
- oracle数据库学习笔记(二)之分组统计查询
- Mybatis学习笔记(二)-----查询sql的配置文件
- ajax瀑布流
- android调用系统播放器后返回的图片旋转了
- C++ template可变参数的一次尝试
- Toolbar使用总结
- Oracle 的 SQL语句中 decode()函数
- mangoDB学习笔记(二)查询
- Hadoop单机模式和伪分布式搭建教程CentOS
- AndroidStudio的常用快捷键
- HTML 第六章----------- 制作聚美优品热点产品列表
- 数据库建表原则
- 深度行研:危机四伏的传统健身馆,如何杀出一条血路
- live555MediaServer搭建RTSP服务器
- 顺序表的C语言实现
- Oracle 11G 数据库 安装 先决条件失败 解决办法