MongoDB Learning notes < six >

Source: Internet
Author: User

Continue the learning of MongoDB

--Index specific explanation

--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. Areas of need for attention

-the creation of an index affects the performance of the insert at the same time that it improves query performance

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

-Indexing every key does not necessarily improve performance

-When doing sort work, assume that large data volumes can also be considered for indexing

3. The name of the index can be specified at the same time when the index is created

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

4. Unique index

How to solve a collection books cannot insert repeated documents

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

5. Reject repeated values

What if there are already repeated 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 view the status information of which index and query data is used in this query

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

8. The index creation process runs 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 &lt; six &gt;

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.