MongoDB Learning Notes < six >

Source: Internet
Author: User

Continue the learning of MongoDB

--Detailed index

--Index management

--Spatial Index

1. Create a simple index

(1) Prepare 200,000 data first

for (var i = 0;i< 200000;i++) {

Db.books.insert ("number": I, "name": i+ "book")

}

(2) Check the query performance

var start = new Date ()

Db.books.find ({"Number": 123456})

var end = new Date ();

End-start

(3) Create an index for number

Db.books.ensureIndex ({"Number":1}) Here 1 stands for Positive order, 1 for reverse

2. Places to be aware of

-index creation can affect insert performance while improving query performance

-for documents that are frequently queried for less insertions, consider using an index

-Indexing each key does not necessarily improve performance

-When doing sort work, the index can also be considered if it is a large amount of data

3. You can specify the name of the index at the same time when creating an index

Db.books.ensureIndex ({"Name": 1},{name: "BookName"})

4. Unique index

How to troubleshoot a collection books cannot insert duplicate documents

Create a unique index: Db.books.ensureIndex ({"Name": 1},{unique:true})

5. Remove duplicate values

What if you already have duplicate documents before creating a unique index?

Db.books.ensureIndex ({"Name": 1},{unique:true,dropdups:true})

6.hint

How to force a query to use the specified index

Db.books.find ({"Name": "Obook"}). Hint ({"name": 1})

The specified index must be a well-created index

7.explain

How to see in detail which index to use for this query and the status information for querying data

Db.books.find ({"Name": "0book"}). Explain ()

8. The creation process of the index is performed in the background

Db.books.ensureIndex ({"Name": "0book"},{bookground:true})

9. Deleting an index

Db.runcommand ({dropindexes: "Books", Index: "Name_1"}) exact deletion

Db.runcommand ({dropindexes: "Books", Index: "*"}) all deleted




MongoDB Learning Notes < six >

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.