MONGODB CREATE Index

Source: Internet
Author: User

MONGODB CREATE Index

> Db.foo.ensureIndex ({"X": 1, "Y":-1})

Use Ensureindex to create indexes, 1 for ascending, 1 for reverse

Tip: Query to return more than half of the results in the collection, but there is no need to create an index, directly scanned data will be more efficient

> Db.test.ensureIndex ({"X": 1}, {"Unique": true})

The above statement is to create a unique index , there may be duplicate data before creating a unique index, we can use "dropdups" to recklessly delete these duplicate data, write the following

> Db.test.ensureIndex ({"X": 1}, {"Unique": true, "dropdups": true})

Of course, it's better to write a script to preprocess it. This data is a better solution for creating a unique index

When creating an index, we can create a name for the cable instead of a name that is automatically generated by the system, using the following notation:

> Db.test.ensureIndex ({"X": 1}, {"Name": "Myindex"})

When we have a large collection of data, creating an index is a time-consuming and laborious task, and we can write the statement that creates the index, which is done in the background by the process of creating the index:

> Db.test.ensureIndex ({"X": 1}, {"Background": true})

When the index is no longer used we can use dropindexes to delete the index, as follows:

> Db.runcommand ({"dropindexes": "Test", "index": "Myindex"})

The parameter followed by the Dropindexes is the collection name, and the parameter followed by index is the name of the indexed, and if you need to delete all indexes, use "*" to

MONGODB CREATE Index

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.