First, the index base:
MongoDB's index is almost exactly the same as the traditional relational database, which includes some basic optimization techniques. Here is the command to create the index:
Copy Code code as follows:
> Db.test.ensureIndex ({"username": 1})
You can see whether the index was successfully established by the following name:
Copy Code code as follows:
The command to delete an index is:
Copy Code code as follows:
> Db.test.dropIndex ({"username": 1})
In MongoDB, we can also create composite indexes, such as:
Copy Code code as follows:
--The number 1 indicates that the index of the username key is stored in ascending order, and 1 indicates that the index of the age key is stored in descending fashion.
> Db.test.ensureIndex ({"username": 1, "Age":-1})
After the index is created, queries based on username and age will use the index, or the index will be used for username based queries, but the composite index will not be used for queries based on age only. So you can say that if you want to use a composite index, you must include the first n indexed columns in the composite index in the query criteria. However, if the order of the key values in the query condition is inconsistent with the order of creation in the composite index, MongoDB can intelligently help us adjust the order so that the composite index can be used for the query. Such as:
Copy Code code as follows:
> Db.test.find ({"Age": "username": "Stephen"})
For the query criteria in the previous example, MongoDB will dynamically adjust the order of the query criteria document before it is retrieved so that the query can use the composite index that you just created.
We can create an index for an inline document with rules and normal documents that don't make any difference, such as:
Copy Code code as follows:
> Db.test.ensureIndex ({"Comments.date": 1})
For the index created above, MongoDB automatically assigns an index name to the newly created index based on the index's KeyName and index direction, and the following command can specify the index name for the index when it is created, such as:
Copy Code code as follows:
> Db.test.ensureIndex ({"username": 1},{"name": "Testindex"})
As the collection grows, it needs to be indexed for a large number of sorts in the query. If there is no key call to the index SORT,MONGODB need to extract all the data into memory and sort. Therefore, if the data is too large to be sorted in memory when doing an index-free sort, MongoDB will make an error.
Second, unique index:
Indexes that are created by default are not unique indexes. The following example creates a unique index, such as:
Copy Code code as follows:
> Db.test.ensureIndex ({"userid": 1},{"unique": true})
If you insert a UserID duplicate document again, MongoDB will complain, prompting you to insert a duplicate key, such as:
Copy Code code as follows:
> Db.test.insert ({"userid": 5})
> Db.test.insert ({"userid": 5})
E11000 duplicate key error index:test.test. $userid _1 dup key: {: 5.0}
If the inserted document does not contain the UserID key, the value of the key in the document is NULL, and if you insert a similar document multiple times, MongoDB will report the same error, such as:
Copy Code code as follows:
> Db.test.insert ({"Userid1": 5})
> Db.test.insert ({"Userid1": 5})
E11000 duplicate key error index:test.test. $userid _1 dup key: {: null}
If duplicates are already present when creating a unique index, we can use the following command to help us eliminate duplicate documents when creating unique indexes, preserving only the first document found, such as:
--First delete the unique index you just created.
Copy Code code as follows:
> Db.test.dropIndex ({"userid": 1})
--Inserts test data to ensure that duplicate keys exist in the collection.
> Db.test.remove ()
> Db.test.insert ({"userid": 5})
> Db.test.insert ({"userid": 5})
-Create a unique index and eliminate duplicate data.
> Db.test.ensureIndex ({"userid": 1},{"unique": true, "dropdups": true})
--The query results confirm that the duplicate key was actually deleted when the index was created.
> Db.test.find ()
{"_id": ObjectId ("4fe823c180144abd15acd52e"), "userid": 5}
We can also create a composite unique index, which guarantees that the composite key value is unique. Such as:
Copy Code code as follows:
> Db.test.ensureIndex ({"userid": 1, "Age": 1},{"unique": true})
Third, the use of explain:
Explain is a very useful tool that will help you get a lot of useful information about the query. The query details can be obtained as long as the method is invoked on the cursor. Explain returns a document, not the cursor itself. Such as:
Copy Code code as follows:
> Db.test.find (). Explain ()
{
"Cursor": "Basiccursor",
"Nscanned": 1,
"Nscannedobjects": 1,
"N": 1,
"Millis": 0,
"Nyields": 0,
"Nchunkskips": 0,
"Ismultikey": false,
"Indexonly": false,
"Indexbounds": {
}
}
Explain returns the index used by the query, the time consuming, and the number of documents scanned for statistical information.
"Cursor": "basiccursor" means no indexes are used.
"nscanned": 1 indicates how many documents are queried.
"N": 1 indicates the number of documents returned.
"Millis": 0 Represents the time consuming of an entire query.
Iv. index management:
The System.indexes collection contains detailed information for each index, so you can query for an existing index by using the following command, such as:
Copy Code code as follows:
> Db.system.indexes.find ()
If you are creating an index for a document that already has data, you can execute the following command so that MongoDB creates the index in the background, so that it does not block other operations when it is created. However, creating an index in a blocking manner can make the entire creation process more efficient, but MongoDB will not be able to receive other actions when it is created.
Copy Code code as follows:
> Db.test.ensureIndex ({"username": 1},{"Background": true})