forked from 0voice/interview_internal_reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
不使用自增是分布式维护起来非常困难。使用ObjectId可以保证不同机器都能用全局唯一的同种方法生成它并且确保不重复 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
索引是为了解决数据搜索效率低下引入的一种特殊的数据结构。索引存储在一个易于遍历读取的数据集合中,索引是对数据库表中一列或多列的值进行排序的一种结构。简单的说,索引就是将`文档`按照某个(或某些)字段顺序组织起来,以便能根据该字段高效的查询。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. 其他索引 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters