MongoDB 索引和事务官网翻译

这是一篇仅用于自身练习翻译的文章以及记录对mongodb相关的一些理解,如有错误欢迎指正。

嗯。谷歌机翻真的差的要命。

网址:https://docs.mongodb.com/manual/indexes/

首先对于索引,通常是用来提高查询效率的。
对于企业来说,一般查询速度慢的时候,问题大多在于索引,在没有索引的情况下,可能查询需要进行全表搜索,在数据量爆炸式增长的时候这种方式就会引起请求挂起,就会卡,或者说也有可能是因为I/O操作太多导致cpu负担不起。对于这两种情况,第一中我们可以进行索引建立,针对性的去进行查询,尽量避免没有索引的全表查询。对于第二种情况,我们可以减少不必要的I/O操作,甚至说我们代码层面会出现问题,比如我们可以使用update来代替findAndModify这种操作,在此一提,findAndModify是在底层进行了一层包装来进行两次操作。在此基础上,我们可以把一些相对来说不常用的数据(如一年前的消费记录等)放置于冷数据库中分开,这样会使查询变得高效。通常我们是不推荐分片以及没有索引的aggragate和mapreduce操作。分片会使一些操作变得更加复杂,如果我们使用时间戳进行分片,那么也就是说在当前月份或年费的操作会远远超过其余分片,这是比较不合理的。当然,我们可以使用时间戳为节点进行分片并且使用hash进行处理,但是这样我们需要在代码层面进行处理跨月份之间的查询。并且我们在进行查询的时候应该限定主从数据库。从数据库用来读,而主数据库用来写。我们也可以在MongoDB上建一层redis缓存(键值对的查询相对于mongodb会快得多),甚至我们可以再加一层本地缓存。然后将一些不常用的我们认为缓存就能获取正确数据的操作放在这两层中避免对MongoDB增加负担。如果在生产环境下更新索引,那么我们可以挑选低峰值时期进行(注意:生产环境下的MongoDB的model只能增不能减)。

下面是翻译部分

Indexes

Indexes support the efficient execution of queries in MongoDB. 
Without indexes, MongoDB must perform a collection scan, i.e. scan every 
document in a collection, to select those documents that match the query 
statement. If an appropriate index exists for a query, MongoDB can use the 
index to limit the number of documents it must inspect.
索引支持在MongoDB高效率查询。没有索引,MongoDB必须进行扫描来匹配文档。如果存在合适的索引,MongoDB可以通过索引来限制他必须查询的文档数量。
Indexes are special data structures
 (https://docs.mongodb.com/manual/indexes/#b-tree) 
that store a small portion of the collection’s data set in an easy to traverse form. 
The index stores the value of a specific field or set of fields, ordered by the 
value of the field. 
The ordering of the index entries supports efficient equality matches and 
range-based query operations. 
In addition, MongoDB can return sorted results by using the ordering in the index.

索引是一种特殊的数据结构,它以易于便利的形式来存储集合数据集的一小部分。索引按字段值排序存储特定字段或字段集的值。索引条目的排序支持有效的等式匹配和机遇范围的查询操作。此外,mongodb可以通过索引顺序返回排序结果。

The following diagram illustrates a query that selects and orders the matching documents using an index:

下图演示了一次使用索引查询和选择匹配的文档


image.png
Fundamentally, indexes in MongoDB are similar to indexes in other database systems. 
MongoDB defines indexes at the [collection]
(https://docs.mongodb.com/manual/reference/glossary/#term-collection) 
level and supports indexes on any field or sub-field of the documents 
in a MongoDB collection.

本质上,MongoDB的索引和其他的数据库类似。MongoDB在集合层面定义索引,并支持MongoDB集合文档的任何字段或子字段的索引。

Default _id Index
默认_id索引
MongoDB creates a [unique index]
(https://docs.mongodb.com/manual/core/index-unique/#index-type-unique) 
on the [_id](https://docs.mongodb.com/manual/core/document/#document-id-field)
field during the creation of a collection. 
The `_id`index prevents clients from inserting two documents with the same 
value for the `_id` field. 
You cannot drop this index on the `_id` field.

MongoDB在创建集合时便在_id上建立一个第一无二的索引。_id索引可防止再插入另一个相同的_id索引。注意:你不能删除_id索引。

NOTE

In [sharded clusters](https://docs.mongodb.
com/manual/reference/glossary/#term-sharded-cluster),
 if you do *not* use the `_id` field as the [shard key]
(https://docs.mongodb.com/manual/reference/glossary/#term-shard-key), 
then your application**must** ensure the uniqueness of the values 
in the `_id` field to prevent errors. 
This is most-often done by using a standard auto-generated [ObjectId]
(https://docs.mongodb.com/manual/reference/glossary/#term-objectid).

注意:在分片集群中,如果你不使用_id作为分片键的话,你的应用必须保证_id的唯一性以防出错。大多数情况下会使用自动生成的随机ObjectId来保证不会出错。

Create an Index

建立索引

Mongo Shell

create an index
在MongoDB Shell中使用 db.collection.createIndex()建立索引
db.collection.createIndex( <key and index type specification>, <options> )

例如

db.collection.createIndex( { name: -1 } )

db.collection.createIndex如果相同规范的索引尚不存在,则该方法仅创建索引。

MongoDB索引使用B树数据结构。
Node.js创建索引
collection.createIndex( { <key and index type specification> }, function(err, result) {
   console.log(result);
   callback(result);
}

例如

collection.createIndex( { name : -1 }, function(err, result) {
   console.log(result);
   callback(result);
}
Index Types
索引类型
MongoDB provides a number of different index types 
to support specific types of data and queries.

MongoDB提供一系列不同的索引类型来支持特定类型的数据和查询

Single Field
单索引
In addition to the MongoDB-defined `_id` index, MongoDB supports 
the creation of user-defined ascending/descending indexes 
on a [single field of a document]
(https://docs.mongodb.com/manual/core/index-single/).

除了MongoDB定义的_id索引之外,MongoDB支持在文档的单索引上建立升序或降序的索引


Single Field
For a single-field index and sort operations, 
the sort order (i.e. ascending or descending) of the index key does not matter 
because MongoDB can traverse the index in either direction.

对于单索引和排序操作来说,索引的排序顺序(升序或者降序)无关紧要因为MongoDB可以在任一方向上便利索引。

Compound Index
复合索引
MongoDB also supports user-defined indexes on multiple fields, 
i.e. [compound indexes](https://docs.mongodb.com/manual/core/index-compound/).

MongoDB也支持用户定义的多字段索引,即复合索引。

The order of fields listed in a compound index has significance. 
For instance, if a compound index consists of { userid: 1, score: -1 }, 
the index sorts first by userid and then, within each userid value, sorts by score.

复合索引中的字段顺序具有重要意义。例如,如果一个复合索引{userid: 1, score: -1},那么首先先由userid进行排序,然后在由score进行排序


Compound Index
For compound indexes and sort operations, 
the sort order (i.e. ascending or descending) of the index keys can determine 
whether the index can support a sort operation. 

对于复合索引和排序操作,索引建的排序顺序(升序或者降序)可以确定索引是否支持排序操作。

Multikey Index

多键索引

MongoDB uses [multikey indexes] to index the content stored in arrays. If you index a field that holds an array value, MongoDB creates separate index entries for *every* element of the array. These [multikey indexes]allow queries to select documents that contain arrays by matching on element or elements of the arrays. MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type.

MongoDB使用多键索引来索引存储在数组中的内容。如果索引包含数组值的字段,MongoDB会为数组的每个元素单独创建条目。多键索引允许查询通过匹配数组的元素或元素来选择包含数组的文档。如果索引字段包含数组值,MongoDB会自动确定是否创建多键索引,不需要你显式的指定。


Multikey Index

Geospatial Index

地理位置索引

To support efficient queries of geospatial coordinate data, 
MongoDB provides two special indexes:
[2d indexes]that uses planar geometry when returning results and 
[2dsphere indexes] that use spherical geometry to return results.

为了支持对地理空间坐标的高效查询,MongoDB提供了两个特殊索引:在返回结果时使用平面几何的2d索引和使用球形几何的2dphere索引。

Text Indexes

文字索引

MongoDB provides a text index type 
that supports searching for string content in a collection.
 These text indexes do not store language-specific stop words 
(e.g. “the”, “a”, “or”) and stem the words in a collection to only store root words.

MongoDB提供一种text索引,支持在集合中搜索字符串内容。这些文字索引不储存特定语言的停用词(如:the, a, or),并且阻止集合中的单词仅存储根词。

Hashed Indexes

哈希索引

To support [hash based sharding], MongoDB provides a [hashed index]type, 
which indexes the hash of the value of a field. 
These indexes have a more random distribution of values along their range, 
but *only* support equality matches and cannot support range-based queries.

为了支持基于哈希的分片,MongoDB提供了哈希索引,该类型索引字段的哈希值。这些索引在范围内具有更加随机的值分布,但是仅仅支持相等匹配,并且不支持基于范围的查询。

Index Properties

索引属性

Unique Indexes

唯一索引

The [unique]property for an index causes MongoDB to reject duplicate values for the indexed field. Other than the unique constraint, unique indexes are functionally interchangeable with other MongoDB indexes.

索引的唯一属性会导致MongoDB拒绝索引字段的重复值。除了唯一约束之外,唯一索引在功能上可与其他MongoDB索引互换

Partial Indexes

部分索引

[Partial indexes] only index the documents in a collection that meet a specified filter expression. By indexing a subset of the documents in a collection, partial indexes have lower storage requirements and reduced performance costs for index creation and maintenance.

部分索引仅索引复合过滤表达式的集合中的文档。通过索引索引文档中集合的子集,部分索引具有较低的存储要求并且索引创建和维护的性能成本。

Partial indexes offer a superset of the functionality of sparse indexes and should be preferred over sparse indexes.

部分索引提供了稀疏索引功能的超集,应该优先于系数索引。

TTL Indexes

TTL索引

[TTL indexes]are special indexes that MongoDB can use to automatically remove documents from a collection after a certain amount of time. This is ideal for certain types of information like machine generated event data, logs, and session information that only need to persist in a database for a finite amount of time.

TTL索引是一种特别的索引,他能在特定的时间后自动移除集合中的文档。这对于某些类型的信息非常理想,例如机器生成的事件数据、日志和会话信息,这些信息只需要在数据库中持续有限的时间。

Index Use

索引使用

Indexes can improve the efficiency of read operations. 

索引可以改善读取操作的效率。

Indexes and Collation

索引和校对

[Collation]allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks.

排序规则允许用户为字符串比较指定特定于语言的规则,例如字母和重音标记的规则。

Mongo Shell

To use an index for string comparisons, an operation must also specify the same collation. That is, an index with a collation cannot support an operation that performs string comparisons on the indexed fields if the operation specifies a different collation.

为了使用索引进行字符串比较,操作必须指定详细的排序规则。也就是,如果制定了不同的排序规则,那么具有排序规则的索引不能支持对索引字段执行字符串比较的操作。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容