MongoDB provides diverse index support. The index information is stored in system. indexes, and the index is always created for _ ID by default. Its Index usage is basically the same as that of relational databases such as MySQL. In fact, it can be said that indexes are another layer of system above the data storage system. Therefore, it is not surprising that storage with different structures has the same or similar index implementation and interfaces.
Basic index
Create an index on the field age, 1 (ascending);-1 (descending)
DB. t3.ensureindex ({age: 1 })
DB. t3.getindexes ();
_ Id is the index automatically created when a table is created. This index cannot be deleted. When the system has a large amount of data, creating an index is very time-consuming. We can execute it in the background by specifying "backgroud: true.
DB. t3.ensureindex ({age: 1}, {backgroud: true })
Document Index
Indexes can be any type of fields or even documents
Create an index on the ADDR Column
DB. Factories. ensureindex ({ADDR: 1 });
The following query will use the index We just created
DB. Factories. Find ({ADDR: {City: "Beijing", state: "BJ "}});
However, the following query will not use indexes, because the query order is different from the index creation order.
DB. Factories. Find ({ADDR: {state: "BJ", city: "Beijing "}});
Composite Index
Like other database products, MongoDB also has a composite index. Next we will create a composite index on ADDR. City and ADDR. state. When you create a composite index, the value 1 next to the field indicates the ascending order, and the value-1 indicates the descending order.
-1 is mainly related to sorting or query within a specified range.
DB. Factories. ensureindex ({"ADDR. City": 1, "ADDR. State": 1 });
// This index is used in the following queries.
DB. factories. find ({"ADDR. city ":" Beijing "," ADDR. state ":" BJ "}); dB. factories. find ({"ADDR. city ":" Beijing "}); dB. factories. find (). sort ({"ADDR. city ": 1," ADDR. state ": 1 });
DB. Factories. Find (). Sort ({"ADDR. City": 1 })
Unique Index
You only need to specify "unique: True" in the ensureindex command to create a unique index.
DB. t4.ensureindex ({firstname: 1, lastname: 1 },{ unique: true });
Use index forcibly
DB. t5.find ({age: {$ LT: 30}). Hint ({Name: 1, age: 1 })
Delete Index
Delete all indexes in Table T3
DB. t3.dropindexes ()
Delete the firstname index in Table T4
DB. t4.dropindex ({firstname: 1 })
Explain execution plan
MongoDB provides an explain command to let us know how the system processes query requests. Using the explain command, we can observe how the system uses indexes to accelerate the search and optimize the INDEX accordingly.
DB. t5.find ({age: {$ GT: 45 }}, {Name: 1}). Explain ()
{"Cursor": "btreecursor age_1", "nscanned": 0, "nscannedobjects": 0, "N": 0, "millis": 0, "nyields": 0, "nchunkskips": 0, "ismultikey": false, "indexonly": false, "indexbounds": {"Age": [[45, 1.7976931348623157e + 308]
}
}
Field description
Cursor: the type of the returned cursor (basiccursor or btreecursor)
Nscanned: Number of scanned documents
N: number of returned documents
Millis: Time consumed (in milliseconds)
Indexbounds: Used Index