Skip to content

Commit

Permalink
add two doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Nov 19, 2019
1 parent 62b2097 commit 916883b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
不使用自增是分布式维护起来非常困难。使用ObjectId可以保证不同机器都能用全局唯一的同种方法生成它并且确保不重复
2 changes: 2 additions & 0 deletions 11.MongoDB篇/11.4.5 在MongoDb中什么是索引?.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
索引是为了解决数据搜索效率低下引入的一种特殊的数据结构。索引存储在一个易于遍历读取的数据集合中,索引是对数据库表中一列或多列的值进行排序的一种结构。简单的说,索引就是将`文档`按照某个(或某些)字段顺序组织起来,以便能根据该字段高效的查询。

30 changes: 30 additions & 0 deletions 11.MongoDB篇/11.4.6 如何添加索引?.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
MongoDB支持多种类型的索引,包括单字段索引、复合索引、多key索引、文本索引等,每种类型的索引有不同的使用场合。

按照类型可分为:

1. 单字段索引

```db.person.createIndex( {age: 1} ) ```

`person`集合建立对`age`的索引。

`{age: 1}` 代表升序索引,也可以通过`{age: -1}`来指定降序索引,对于单字段索引,升序/降序效果是一样的。

2. 复合索引

``` db.person.createIndex( {age: 1, name: 1} ) ```

他是单字段索引的升级,可以对多个字段进行索引。按第一个字段排序,第一个字段相同的文档按第二个字段排序。

3. 多key索引

```
{"name" : "jack", "age" : 19, habbit: ["football, runnning"]}
db.person.createIndex( {habbit: 1} ) // 自动创建多key索引
db.person.find( {habbit: "football"} )
```

当索引的字段为数组时,创建出的索引称为多key索引,多key索引会为数组的每个元素建立一条索引,比如person表加入一个`habbit`字段(数组)用于描述兴趣爱好,需要查询有相同兴趣爱好的人就可以利用`habbit`字段的多key索引。

4. 其他索引

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@

##### [11.2.2 非关系型数据库有哪些类型?](11.MongoDB篇/11.2.2%20%E9%9D%9E%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93%E6%9C%89%E5%93%AA%E4%BA%9B%E7%B1%BB%E5%9E%8B%EF%BC%9F.md)

##### [11.2.3 为什么用MOngoDB](11.MongoDB篇/11.2.3%20%E4%B8%BA%E4%BB%80%E4%B9%88%E7%94%A8MOngoDB%EF%BC%9F.md)
##### [11.2.3 为什么用MongoDB](11.MongoDB篇/11.2.3%20%E4%B8%BA%E4%BB%80%E4%B9%88%E7%94%A8MOngoDB%EF%BC%9F.md)

##### [11.2.4 在哪些场景使用MongoDB?](11.MongoDB篇/11.2.4%20%E5%9C%A8%E5%93%AA%E4%BA%9B%E5%9C%BA%E6%99%AF%E4%BD%BF%E7%94%A8MongoDB%EF%BC%9F.md)

Expand Down Expand Up @@ -807,7 +807,7 @@

##### [11.4.4 “ObjectID”有哪些部分组成?](11.MongoDB篇/11.4.4%20%E2%80%9CObjectID%E2%80%9D%E6%9C%89%E5%93%AA%E4%BA%9B%E9%83%A8%E5%88%86%E7%BB%84%E6%88%90%EF%BC%9F.md)

##### [11.4.5 在MongoDb中什么是索引](11.MongoDB篇/11.4.5%20%E5%9C%A8MongoDb%E4%B8%AD%E4%BB%80%E4%B9%88%E6%98%AF%E7%B4%A2%E5%BC%95%EF%BC%9F.md)
##### [11.4.5 在MongoDB中什么是索引](11.MongoDB篇/11.4.5%20%E5%9C%A8MongoDb%E4%B8%AD%E4%BB%80%E4%B9%88%E6%98%AF%E7%B4%A2%E5%BC%95%EF%BC%9F.md)

##### [11.4.6 如何添加索引?](11.MongoDB篇/11.4.6%20%E5%A6%82%E4%BD%95%E6%B7%BB%E5%8A%A0%E7%B4%A2%E5%BC%95%EF%BC%9F.md)

Expand Down

0 comments on commit 916883b

Please sign in to comment.