Index of MongoDB Learning notes (goto)

Source: Internet
Author: User
Tags goto index sort

First, the index base:
MongoDB's index is almost identical to the traditional relational database, which includes some basic optimization techniques. Here is the command to create the index:
> Db.test.ensureIndex ({"username": 1})
You can see whether the index has been successfully established by the following name:
> db.test.getIndexes ()
The command to delete an index is:
> Db.test.dropIndex ({"username": 1})
In MongoDB, we can also create composite indexes, such as:
--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 form.
> Db.test.ensureIndex ({"username": 1, "Age":-1})
After the index is created, queries based on username and age will use the index, or username-based queries will use the index, but only the age-based query would not use the composite index. So you can say that if you want to use a composite index, you must include the first n index columns in the composite index in the query criteria. However, if the order of key values in the query condition is inconsistent with the order of creation in the composite index, MongoDB can intelligently help us adjust that order so that the composite index can be used by the query. Such as:
> Db.test.find ({"Age": +, "username": "Stephen"})
For the query criteria in the example above, MongoDB will dynamically adjust the order of the query criteria document before retrieving it so that the query can use the composite index that was just created.
We can create an index for an inline document with no differences between the rules and the normal document, such as:
> 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 assigns an index name to the index when it is created, such as:
> Db.test.ensureIndex ({"username": 1},{"name": "Testindex"})
As the collection grows, you need to index a large number of sorts in the query. If there is no key call to the index, SORT,MONGODB needs to extract all the data into memory and sort. So when you do a no-index sort, if the amount of data is too large to sort in memory, MongoDB will error.

Second, unique index:
Indexes created by default are not unique indexes. The following example creates a unique index, such as:
> Db.test.ensureIndex ({"userid": 1},{"unique": true})
If you insert the UserID duplicate document again, MongoDB will make an error to prompt you to insert a duplicate key, such as:
> 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 a UserID key, the value of the key in the document is null, and if a similar document is inserted more than once, MongoDB will report the same error, such as:
> Db.test.insert ({"Userid1": 5})
> Db.test.insert ({"Userid1": 5})
E11000 duplicate key error index:test.test. $userid _1 dup key: {: null}
If duplicate entries already exist when creating a unique index, we can help us to eliminate duplicate documents when creating a unique index, preserving only the first document found, such as:
--First delete the unique index you just created.
> Db.test.dropIndex ({"userid": 1})
-Insert 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 result confirms 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:
> 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 your queries. As long as the method is called on the cursor, the query details can be obtained. Explain returns a document instead of the cursor itself. Such as:
> 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 statistics for the index used by the query, time-consuming, and number of scanned documents.
"cursor": "Basiccursor" means that the index is not used.
"nscanned": 1 indicates the number of documents queried.
"n": 1 indicates the number of documents returned.
"Millis": 0 Represents the time-consuming whole 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:
> Db.system.indexes.find ()
If you are creating an index on 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 way can make the entire creation process more efficient, but MongoDB will not be able to receive other operations when it is created.
> Db.test.ensureIndex ({"username": 1},{"Background": true})

Original http://www.cnblogs.com/stephen-liu74/archive/2012/08/01/2561557.html

Index of MongoDB Learning notes (goto)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.