From d09df055c833cda487e09851d0d08b4da0661927 Mon Sep 17 00:00:00 2001 From: heibaiying <2806718453@qq.com> Date: Wed, 31 Jul 2019 14:39:37 +0800 Subject: [PATCH] mongodb --- README.md | 9 +- notes/{Docker基础.md => Docker_基础.md} | 0 ....md => Java_单例设计模式详解.md} | 0 notes/{Java反射.md => Java_反射.md} | 0 notes/{Java并发.md => Java_并发.md} | 0 ...ll命令.md => Linux_常用Shell命令.md} | 0 notes/MongoDB_基础.md | 59 ++-- notes/MongoDB_索引.md | 27 +- notes/MongoDB_聚合.md | 321 ++++++++++++++++-- .../{RabbitMQ简介.md => RabbitMQ_基础.md} | 0 10 files changed, 361 insertions(+), 55 deletions(-) rename notes/{Docker基础.md => Docker_基础.md} (100%) rename notes/{Java单例设计模式详解.md => Java_单例设计模式详解.md} (100%) rename notes/{Java反射.md => Java_反射.md} (100%) rename notes/{Java并发.md => Java_并发.md} (100%) rename notes/{Linux常用Shell命令.md => Linux_常用Shell命令.md} (100%) rename notes/{RabbitMQ简介.md => RabbitMQ_基础.md} (100%) diff --git a/README.md b/README.md index 75ea087..794ff35 100644 --- a/README.md +++ b/README.md @@ -54,10 +54,11 @@ TODO #### 4.MongoDB -+ MongoDB 简介及基本原理 -+ MongoDB数据类型分析 -+ MongoDB 聚合、索引及基本执行命令 -+ MongoDB数据分片、转存及恢复策略 ++ [MongoDB 基础](https://github.com/heibaiying/Full-Stack-Notes/blob/master/notes/MongoDB_基础.md) ++ [MongoDB 索引](https://github.com/heibaiying/Full-Stack-Notes/blob/master/notes/MongoDB_索引.md) ++ [MongoDB 聚合](https://github.com/heibaiying/Full-Stack-Notes/blob/master/notes/MongoDB_聚合.md) ++ [MongoDB 复制](https://github.com/heibaiying/Full-Stack-Notes/blob/master/notes/MongoDB_复制.md) ++ [MongoDB 分片](https://github.com/heibaiying/Full-Stack-Notes/blob/master/notes/MongoDB_分片.md) diff --git a/notes/Docker基础.md b/notes/Docker_基础.md similarity index 100% rename from notes/Docker基础.md rename to notes/Docker_基础.md diff --git a/notes/Java单例设计模式详解.md b/notes/Java_单例设计模式详解.md similarity index 100% rename from notes/Java单例设计模式详解.md rename to notes/Java_单例设计模式详解.md diff --git a/notes/Java反射.md b/notes/Java_反射.md similarity index 100% rename from notes/Java反射.md rename to notes/Java_反射.md diff --git a/notes/Java并发.md b/notes/Java_并发.md similarity index 100% rename from notes/Java并发.md rename to notes/Java_并发.md diff --git a/notes/Linux常用Shell命令.md b/notes/Linux_常用Shell命令.md similarity index 100% rename from notes/Linux常用Shell命令.md rename to notes/Linux_常用Shell命令.md diff --git a/notes/MongoDB_基础.md b/notes/MongoDB_基础.md index b23c81b..92a4885 100644 --- a/notes/MongoDB_基础.md +++ b/notes/MongoDB_基础.md @@ -1,5 +1,22 @@ # MongoDB 基础 + + ## 一、数据类型 ### 1.1 BSON @@ -56,7 +73,7 @@ db.collection.insertMany() 新增单条数据的示例如下。额外需要说明的是,在插入文档前,文档所属的集合不必预先创建,程序会自动创建: -```json +```shell db.user.insertOne({ name: "heibai", age: 26, @@ -68,7 +85,7 @@ db.user.insertOne({ 新增多条数据的示例如下: -```json +```shell db.user.insertMany([ { name: "hei", @@ -109,7 +126,7 @@ db.collection.find(, ) + ``:用于指定查询条件,不加任何条件则默认查询集合中全部数据; + ``:可选操作,用于自定查询的返回字段,1 表示该字段包含在返回结果中,0 表示不返回,示例如下: -```json +```shell db.user.find({},{name: 1, ObjectId:-1})} ``` @@ -123,19 +140,19 @@ db.user.find({name:"heibai"}) 上面的语法实际上是 `$eq` 操作的简写形式,如下: -```json +```shell db.user.find({name: {$eq: "heibai"}}) ``` 所以如果你想要进行非等值查询,则可以使用 `$ne` 操作符,代表 not equal ,示例如下: -```json +```shell db.user.find({name: {$ne: "heibai"}}) ``` 特别的,如果你想允许某个字段等于多个值,可以使用 `$in` 操作符,示例如下: -```json +```shell db.user.find({name: {$in:["heibai","ying"]} }) ``` @@ -143,7 +160,7 @@ db.user.find({name: {$in:["heibai","ying"]} }) Mongodb 提供了比较操作符 `$lt`、`$lte`、`$gt` 和 `$gte` ,分别对应 <、 <=、 >和 >= ,主要用于范围查查询,示例如下: -```json +```shell db.user.find({age: {$gt: 20, $lt: 40}}) ``` @@ -153,18 +170,18 @@ Mongodb 提供了逻辑操作符 `$or`、`$and`、`$not`、`$nor` ,用于处 查询姓名为 heibai 或者年龄大于 30 岁的所有用户,此时可以使用 $or 操作符: -```json +```shell db.user.find( { $or: [{ name: "heibai" }, { age: { $gt: 30 } }] }) ``` 查询所有姓名不是以 hei 开头的所有用户,此时可以使用 $not 操作符来配合正则表达式: -```json +```shell db.user.find({name: {$not: /^hei*/}}) ``` 如果文档中存在 name 字段,则它的值不能为 heibai,如果文档中存在 age 字段,则它的值不能大于 30 ,只有满足以上两个条件的文档才会被查询出来,示例如下: -```json +```shell db.user.find( { $nor: [{ name: "heibai" }, { age: { $gt: 30 } }] }) ``` @@ -174,22 +191,22 @@ $and 操作符的使用率比较低,因为此时更好的方式是把多个条 如果需要查询个人爱好中有 football 的所有用户,即只要集合 Hobby 中存在 football 即可,对应的查询方法如下: -```json +```shell db.user.find({Hobby: "football"}) ``` 如果想要获取集合中指定位置等于指定值的文档,对应的查询方法如下: -```json +```shell db.user.find({"Hobby.2": "football"}) ``` 如果想要约束集合必须包含多个指定值,此时可以使用 $all 操作符: -```json +```shell db.user.find({Hobby:{ $all: ["football", "tennis"]}}) ``` 查询时如果只想返回集合的部分内容,则可以使用 $slice ,$slice 接收一个参数 n,正数表示获取集合的前 n 个参数,负数表示获取集合末尾的 n 个参数,示例如下: -```json +```shell db.user.find({name: "heibai"},{Hobby:{$slice: 2}}) ``` @@ -220,7 +237,7 @@ db.collection.updateMany(, , ) + ``:更改操作或新文档数据; + ` `:可选操作,常用的可选操作是 `upsert` ,当其为 true 时,代表如果按照过滤条件没有找到对应的文档,则将待更改的数据插入到集合中;当其为 false 时,如果没有找到数据,则不执行任何操作。示例如下: -```json +```shell db.user.replaceOne( { _id: ObjectId("5d3d00a4ad383d3becc7b03a")}, { @@ -242,7 +259,7 @@ db.user.replaceOne( 用于修改具体的字段,如果待修改的字段不存在,则会新增该字段。示例如下: -```json +```shell db.user.updateOne( { name: "danrenying"}, { $set: {age: 66} } @@ -253,7 +270,7 @@ db.user.updateOne( 用于对指定字段的值进行增加或减少,示例如下: -```json +```shell db.user.updateOne( { name: "danrenying"}, { $inc: {age: -10} } @@ -268,7 +285,7 @@ db.user.updateOne( 用于往数组中新增数据,示例如下。使用 `$each` 可以一次添加多个元素: -```json +```shell db.user.updateOne( { name: "danrenying"}, { $push: {"Hobby": {$each: ["film","music"]}} } @@ -279,7 +296,7 @@ db.user.updateOne( 该修改器可以把数组当做集 (set) 来使用,即只能添加当前数组中不存在的数据,示例如下: -```json +```shell db.user.updateOne( { name: "danrenying"}, { $addToSet: {"Hobby": {$each: ["film","music"]}} } @@ -290,7 +307,7 @@ db.user.updateOne( 该修改器可以从数组任意一端删除元素,`-1` 代表从数组头删除元素,`1` 代表从数组尾删除元素,示例如下: -```json +```shell db.user.updateOne( { name: "danrenying"}, { $pop: {"Hobby": -1} } @@ -319,7 +336,7 @@ db.collection.deleteOne() 使用示例如下: -```json +```shell db.user.deleteOne( { name: "danrenying"} ) diff --git a/notes/MongoDB_索引.md b/notes/MongoDB_索引.md index b3b2b21..060c0de 100644 --- a/notes/MongoDB_索引.md +++ b/notes/MongoDB_索引.md @@ -1,5 +1,27 @@ # MongoDB 索引 + + ## 一、索引简介 ### 1.1 创建索引 @@ -90,8 +112,7 @@ db.user.find({}).sort({name:1}) 当前大多数数据库都支持双向遍历索引,这和存储结构有关 (如下图)。在 B-Tree 结构的叶子节点上,存储了索引键的值及其对应文档的位置信息,而每个叶子节点间则类似于双向链表,所以如下图既可以从值为 4 的索引遍历到值为 92 的索引,反之亦然。 -![b-tree](D:\Full-Stack-Notes\pictures\b-tree.png) - +
### 2.2 复合索引 @@ -104,7 +125,7 @@ db.user.createIndex( { name: -1,birthday: 1} ) 需要注意的是 MongoDB 的复合索引具备前缀索引的特征,即如果你创建了索引 `{ a:1, b: 1, c: 1, d: 1 }`,那么等价于在该集合上,还存在了以下三个索引,这三个隐式索引同样可以用于优化查询和排序操作: -```json +```shell { a: 1 } { a: 1, b: 1 } { a: 1, b: 1, c: 1 } diff --git a/notes/MongoDB_聚合.md b/notes/MongoDB_聚合.md index 342b9a5..4485248 100644 --- a/notes/MongoDB_聚合.md +++ b/notes/MongoDB_聚合.md @@ -1,5 +1,22 @@ # MongoDB 聚合操作 + + ## 一、聚合简述 在日常开发中,我们通常需要对存储数据进行聚合分析后,再返回给客户端。MongoDB提供了三种聚合的方式,分别是聚合管道,map-reduce 函数和单用途聚合方法。 @@ -59,7 +76,7 @@ db.employees.aggregate([ 所以最后的输出结果如下: -```json +```shell { "_id" : ObjectId("5d3fe6488ba16934ccce999d"), "fullName" : "GeorgiFacello" @@ -70,7 +87,7 @@ db.employees.aggregate([ } ``` -在当前最新的 MongoDB 4.x 中,MongoDB 提供了将近 30 个管道阶段,用于满足不同数据处理的需求。以下主要介绍常用几个管道阶段,如果想要了解全部的管道阶段,可以参见官方文档:[Aggregation Pipeline Stages](https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/) +在当前最新的 MongoDB 4.x 中,MongoDB 提供了将近 30 个管道阶段,用于满足不同数据处理的需求。以下主要介绍常用几个管道阶段,如果想要了解全部的管道阶段,可以参见官方文档:[Aggregation Pipeline Stages](https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/) 。 ### 1.1 $match @@ -117,13 +134,13 @@ db.employees.aggregate([ ]) ``` -这里判断当文档的 hobby 属性为空数组时,则其 hobby 属性不会被输出到下一个管道阶段。 +这里判断当文档的 hobby 属性为空数组时,其 hobby 属性不会被输出到下一个管道阶段。 ### 1.3 $group $group 管道阶段和大多数关系型数据库中的 group by 字句功能类似,都是用于分组计算。示例如下: -```json +```shell db.employees.aggregate( [ { $group : { @@ -199,7 +216,7 @@ $unwind 将文档按照数组中的每一个元素进行拆分,类似于大多 + **preserveNullAndEmptyArrays**:如果用于展开的字段值为 null 或空数组时,则对应的文档不会被输出到下一阶段。如果想要输出到下一阶段则需要将该属性设置为 true。示例语句如下: -```json +```shell db.employees.aggregate( [ {$project: {_id: 0, emp_no: 1, hobby:1}}, { $unwind: @@ -228,7 +245,7 @@ db.employees.aggregate( [ ### 1.5 $sort -$sort 主要用于排序操作,需要注意的是如果可以,应当尽量将该操作放置在管道的第一阶段,从而可以利用索引进行排序,否则就需要使用内存进行排序,这时排序操作就会变得相当昂贵,需要额外的内存和计算资源的开销。 +$sort 主要用于排序操作,需要注意的是如果可以,应当尽量将该操作放置在管道的第一阶段,从而可以利用索引进行排序,否则就需要使用内存进行排序,这时排序操作就会变得相当昂贵,需要额外消耗内存和计算资源。 示例如下: @@ -250,6 +267,29 @@ db.employees.aggregate([ ### 1.8 $lookup +#### 1. 关联查询 + +$lookup 类似与大多数关系型数据库中的 left outer join ,用于实现不同集合之间的连接。其基本的语法如下: + +```shell +{ + $lookup: + { + from: , + localField: , + foreignField: , + as: + } +} +``` + ++ **from**:指定同一数据库中的集合以进行连接操作; ++ **localField**:连接集合中用于进行连接的字段; ++ **foreignField**:待连接集合中用于进行连接的字段; ++ **as**:指定用于存放匹配文档的新数组字段的名称。如果指定的字段已存在,则进行覆盖。 + +为了 $lookup 管道,我们需要额外添加一个集合,用于储存员工的职位信息 (一个员工可以有多个职位头衔) ,语法如下: + ```shell db.titles.insertMany([ { @@ -270,11 +310,13 @@ db.titles.insertMany([ }, { emp_no: 10004, - title: "Senior Engineer " + title: "Senior Engineer" } ]) ``` +执行连接查询,连接条件为员工工号: + ```shell db.employees.aggregate([ { @@ -287,8 +329,177 @@ db.employees.aggregate([ } } ]) +``` +输出结果如下,为节省篇幅和突出重点,这里只显示部分内容,下文亦同。从输出中可以看到员工的职位信息都作为内嵌文档插入到指定的 emp_title 字段下,等价于执行了 left outer join 操作。 +```shell +{ + "_id" : ObjectId("5d3ffaaa8ba16934ccce99a1"), + "emp_no" : 10001, + ........ + "emp_title" : [ + { + "_id" : ObjectId("5d4011728ba16934ccce99a5"), + "emp_no" : 10001, + "title" : "Senior Engineer" + } + ] +}, + +........ + +{ + "_id" : ObjectId("5d3ffaaa8ba16934ccce99a4"), + "emp_no" : 10004, + ........ + "emp_title" : [ + { + "_id" : ObjectId("5d4011728ba16934ccce99a8"), + "emp_no" : 10004, + "title" : "Engineer" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a9"), + "emp_no" : 10004, + "title" : "Senior Engineer" + } + ] +} +``` + +#### 2. 非相关查询 + +除了关联查询外,MongoDB 从 3.6 开始,还支持非相关查询。其基本语法如下: + +```shell +{ + $lookup: + { + from: , + let: { : , …, : }, + pipeline: [ ], + as: + } +} +``` + +- **from**:指定同一数据库中的集合以进行连接操作; +- **let**:可选操作。用于指定在管道阶段中使用的变量,需要注意的是访问 let 变量必须使用 $expr 运算符。 +- **pipeline**:必选值,用于指定要在已连接集合上运行的管道。需要注意的是该管道无法直接访问输入文档中的字段,需要先在 let 中进行定义,然后再引用。 +- **as**:指定用于存放匹配文档的新数组字段的名称。如果指定的字段已存在,则进行覆盖。 + +这里我们分别基于三种场景来讲解不相关查询: + +**场景一**:假设每个员工都需要了解其他员工的职位信息,以便进行沟通,则对应的查询语法如下: + +```shell +db.employees.aggregate([ + { + $lookup: + { + from: "titles", + pipeline: [], + as: "emps_title" + } + } +]) +``` + +此时输出如下,可以看到每个员工的 emps_title 字段都是相同的,都包含了所有员工的职位信息。因为查询中并没有指定任何字段进行关联,所以这种查询也被称为非相关查询。 + +```shell +{ + "_id" : ObjectId("5d3ffaaa8ba16934ccce99a1"), + "emp_no" : 10001, + ....... + ], + "emps_title" : [ + { + "_id" : ObjectId("5d4011728ba16934ccce99a5"), + "emp_no" : 10001, + "title" : "Senior Engineer" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a6"), + "emp_no" : 10002, + "title" : "Staff" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a7"), + "emp_no" : 10003, + "title" : "Senior Engineer" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a8"), + "emp_no" : 10004, + "title" : "Engineer" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a9"), + "emp_no" : 10004, + "title" : "Senior Engineer" + } + ] +}, + +{ + "_id" : ObjectId("5d3ffaaa8ba16934ccce99a2"), + ....... + "emps_title" : [ + { + "_id" : ObjectId("5d4011728ba16934ccce99a5"), + "emp_no" : 10001, + "title" : "Senior Engineer" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a6"), + "emp_no" : 10002, + "title" : "Staff" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a7"), + "emp_no" : 10003, + "title" : "Senior Engineer" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a8"), + "emp_no" : 10004, + "title" : "Engineer" + }, + { + "_id" : ObjectId("5d4011728ba16934ccce99a9"), + "emp_no" : 10004, + "title" : "Senior Engineer" + } + ] +}, + +.......... +``` + +**场景二**:假设每个员工都只需要知道办公室职员 (即职位为 Staff ) 的员工的信息,此时就需要利用 pipeline 管道对 titles 集合中的数据进行过滤,对应的查询语法为: + +```shell +db.employees.aggregate([ + { + $lookup: + { + from: "titles", + pipeline: [ + { $match: + { $expr: { $eq: [ "$title","Staff"]}} + } + ], + as: "emps_title" + } + } +]) +``` + +**场景三**:假设只有男员工需要知道其他职员的信息,此时就需要利用 pipeline 管道对 employees 集合中的数据进行过滤。由于 employees 集合是输入集合,管道无法直接访问其中的字段,此时需要先在 let 中进行定义,然后再引用。语句如下: + +```shell db.employees.aggregate([ { $lookup: @@ -300,29 +511,14 @@ db.employees.aggregate([ { $expr: { $eq: [ "$$gender","M"]}} } ], - as: "emp_title" - } - } -]) - - - -db.employees.aggregate([ - { - $lookup: - { - from: "titles", - pipeline: [ - { $match: - { $expr: { $eq: [ "$title","M"]}} - } - ], - as: "emp_title" + as: "emps_title" } } ]) ``` +这里需要使用两个 `$` 符号对 gender 变量进行引用,因为使用 let 定义后,其类似于系统变量。 + ### 1.9 $out $out 用于将数据写入指定的集合,它必须是管道中的最后一个阶段。如果指定的集合不存在,则会自动新建;如果指定的集合存在,它会覆盖原有集合的数据。其实际的步骤如下: @@ -360,8 +556,79 @@ db.employees.aggregate([ 当排序操作在限制操作之前时,如果没有中间阶段会修改文档数量 (例如 $unwind,$group),优化器会将 $limit 合并到 $sort中。如果在 $sort 和 $limit 之间存在修改文档数量的管道阶段,MongoDB 将不会执行合并。 -了解这些优化策略可以有助于我们在开发中合理设置管道的顺序。想要了解全部的优化策略,可以参阅 MongoDB 的官方文档:[Aggregation Pipeline Optimization](https://docs.mongodb.com/manual/core/aggregation-pipeline-optimization/) +了解这些优化策略可以有助于我们在开发中合理设置管道的顺序。想要了解全部的优化策略,可以参阅 MongoDB 的官方文档:[Aggregation Pipeline Optimization](https://docs.mongodb.com/manual/core/aggregation-pipeline-optimization/) 。 ## 三、MapReduce -## 四、单用途聚合方法 \ No newline at end of file +MongoDB 的 MapReduce 在概念上与 Hadoop MapReduce 类似,MongoDB 将 *map* 阶段应用于每个符合条件的每个输入文档,*map* 阶段的输出结果是 key-value 键值对。对于具有多个值的 key,MongoDB 会执行 *reduce* 阶段,该阶段负责收集并聚合数据,然后将结果存储在集合中。 + +这里我们以按照性别分组计算员工的平均年龄为例,演示 MapReduce 的基本使用,语句如下: + +```shell +db.employees.mapReduce( + function(){ + emit(this.gender,this.age) + }, + function(key,values){ + return Array.avg(values) + }, + { out:"age_avg"} +) +``` + +对应的计算结果会被输出到 age_avg 集合中,其内容如下: + +```shell +{ + "_id" : "F", + "value" : 33 +}, +{ + "_id" : "M", + "value" : 39 +} +``` + +当前 MongoDB 支持在分片集合执行 map-reduce 操作,也支持将 Map-reduce 操作的结果输出到分片集合。map-reduce 使得开发者可以灵活控制聚合行为,但其性能不如聚合管道,所以两者应适场景而选用。 + +## 四、单用途聚合方法 + +出于易用性考虑,MongoDB 提供了部分 API,用于实现对常见聚合过程的简单访问。如下: + +### 4.1 count + +用于计算符合条件的文档的数量,如果想要计算集合中所有文档的数量,则传入空对象即可: + +```shell +db.employees.count({gender:"M"}) +``` + +### 4.2 estimatedDocumentCount + +官方更加推荐使用 estimatedDocumentCount 计算集合中所有文档的数量,它会直接获取元数据信息并返回,因此它并不支持传入查询条件,示例如下: + +```shell +db.employees.estimatedDocumentCount({}) +``` + +### 4.3 distinct + +和大多数数据库中的 distinct 关键字类似,用于返回不重复的数据: + +```shell +db.titles.distinct("title") +``` + + + +## 参考资料 + ++ 官方文档:[Aggregation](https://docs.mongodb.com/manual/aggregation/)、[Map-Reduce](https://docs.mongodb.com/manual/core/map-reduce/) ++ 所有聚合管道总览:https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/ + ++ 聚合管道中所有可选操作符:https://docs.mongodb.com/manual/reference/operator/aggregation/ + + + + + diff --git a/notes/RabbitMQ简介.md b/notes/RabbitMQ_基础.md similarity index 100% rename from notes/RabbitMQ简介.md rename to notes/RabbitMQ_基础.md