First, the index introduction
In MongoDB, indexes are used to support efficient queries. If there is no index, MONGODB must scan each document in the entire collection to find a matching document. However, if an appropriate index is established, MongoDB can limit the number of documents checked by index.
An index is a special data structure that stores a small subset of data sets in a collection that are easily traversed. The index stores the specified field or field collection, which are sorted according to the field values. Sorted index entries can support efficient equivalence matching and range-based query operations, and MongoDB can also return an ordered set of results by sorting the index.
Basically, the index of MongoDB is similar to the index of other relational database, it is defined at the collection level and supports any field or subdomain, it uses B-TREE data structure.
Second, the index concept
1. Index type
MongoDB offers many different types of indexes. For a document or inline document, you can create an index on any field or inline field. Generally, you should create generic user-oriented indexes. Through these indexes, make sure MONGODB scans the least likely to match the document. in the MongoDB shell, you can create an index by calling the CreateIndex () method.
1) Single field index
For documents in a collection, MongoDB fully supports the creation of indexes on any field. By default, there is an index on the _id field of any collection, and the app and the user can also add additional indexes to support important queries and operations. MongoDB supports both single-field indexes and composite indexes that support multiple fields, so here's an example of a single-segment index:
> Db.friends.insert ({"Name": "Alice", "Age":"ninserted": 1 })> Db.friends.createIndex ({" Name ": 1}" #在文档的name字段上建索引 { false, "Numindexesbefore": 1, " Numindexesafter ": 2, " OK ": 1}
Db.collection.createIndex (keys,options) Introduction:
Parameter |
Type |
Description |
keys |
document |
P class= "First" >a document that contains the field and value pairs where the field is the index key and the value describe s the type of index for that field. For the ascending index on a field, specify a value of 1 ; For descending index, specify a value of -1 . mongodb supports several different index types including text , Geospatial , and hashed indexes. see Index Types for more information. |
options |
document |
Optional. A document that contains a set of options that controls the creation of the index. see Options for details. |
- _id field index: When a collection is created, the default is to create an ascending, unique index on the _id field, which cannot be deleted. Given that the _id field is a primary key for a collection of , there should be a unique _id field for each document in the collection, in which you can store any unique value. The default value of the _id field is Objectid, which is automatically generated when the document is inserted. In a Shard collection environment, if you do not specify the _id field Shard key, your application must ensure that the _id fields are unique, otherwise it will be an error. A common practice is to resolve by automatically generating Objectid standard values.
- inline field index: You can also create an index on any field in an inline document, just as you would create it on a document's first-level field. However, it is necessary to note that there is a difference between creating an index on an inline field and creating an index on a nested document, which accesses the field names in the inline document by means of a dot. Take a look at the following example:
> Db.people.insert (... {... Name:"John Doe",... Address: {... " Main ",... ZipCode:"53511",... " WI "" ninserted ": 1> Db.people.createIndex ({" Address.zipcode ": 1}) #通过 Address.zipcode method refers to the ZipCode field, note that double quotation marks are required.
False"Numindexesbefore": 1"Numindexesafter": 2"OK": 1 }
> Db.factories.insert ( {metro: { "New York", State :"NY" }, "Giant Factory" "ninserted": 1 })> Db.factories.createIndex ({metro:1}) { false , " Numindexesbefore ": 1, " Numindexesafter ": 2, " OK ": 1}
The Metro field above is an inline document that contains the inline fields City and state, so the creation method is the same as the one-level field creation method.
The following query can use the index:
Db.factories.find ({metro: {city: ' New York ', State: ' NY '}})
{"_id": ObjectId ("56189565D8624FAFA91CBBC1"), "Metro": {"City": "New York", "state": "NY"}, "name": "Giant Factory " }
When you make an equivalent match query in an inline document, you need to be aware of the order of the fields, for example, the following query will not match any documents:
> Db.factories.find ({metro: {state: ' NY ', City: ' New York '} })
2) Composite Index
"Four" MongoDB index management