Index of MongoDB

Source: Internet
Author: User
Tags create index mongodb mongodb query

Indexes can often greatly improve the efficiency of queries. When using queries in your system, you should consider establishing related indexes.

An index is a data structure that collects the values of a specific field in a set of Chinese documents. The MongoDB query optimizer can

Use this data structure to quickly find and sort documents in a collection (collection) (collection),

To be exact, these indexes are implemented by B-tree indexes.

1. Create a simple index

Data prepares index.js, creates books document and inserts 200,000 data, as follows:

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

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

}

1. Check query Performance First

var start = new Date ()

Db.books.find ({number:65871})

var end = new Date ()

End-start

2. Create an index for number

Db.books.ensureIndex ({number:1})

3. The execution of the first code can be seen as an order of magnitude performance improvement

2. The index uses the place which needs to notice

1. When you create an index, note that 1 is a positive sequence. Create INDEX-1 is reverse-create index

2. Index creation in improving query performance colleagues will affect the performance of inserts for frequently queried documents that are less inserted can be considered for indexing

3. The index should pay attention to the order of the index

4. Indexing every key does not necessarily improve performance. The index is not omnipotent.

5. When doing the sorting work, if it is a large amount of data can also be considered plus index to improve the performance of sorting

3. Create index to specify the name of the index

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

4. Unique index

4.1 How to troubleshoot a document books cannot insert duplicate values

Establish a unique index

Db.books.ensureIndex ({name:-1},{unique:true})

Test

Db.books. Insert ({name: "1book"})

5. Eliminate duplicate values

5.1 If there are duplicate values before a unique index is recommended

Db.books.ensureIndex ({name:-1},{unique:true,dropdups:true})

6.Hint

6.1 How do I force a query to use a specified index?

Db.books.find ({name: "1book", number:1}). Hint ({name:-1})

The specified index must be an index that has already been created

7.Expain

7.1 How to view the status information of this query using that index and query data in detail

Db.books.find ({name: "1book"}). Explain ()

Returns the result as follows:

> Db.books.find ({name: "1book"}). Explain ()

{

"Cursor": "Btreecursor name_1",

"Ismultikey": false,

"N": 1,

"Nscannedobjects": 1,

"Nscanned": 1,

"Nscannedobjectsallplans": 1,

"Nscannedallplans": 1,

"Scanandorder": false,

"Indexonly": false,

"Nyields": 0,

"Nchunkskips": 0,

"Millis": 0,

"Indexbounds": {

' Name ': [

[

"1book",

"1book"

]

]

},

"Server": "pc-20110917qhjt:27017"

}

"Cursor": "Btreecursor name_1" uses the index

"nscanned": 1 to find several documents

"Millis": 0 query time, 0 is very good performance

Second, index management

8, System.indexes

8.1, in the shell, to see the database has been established index. As follows:

Db.system.indexes.find ()

Db.system.namespaces.find ()

9, backstage implementation

9.1, the process of creating the index will temporarily lock the table problem, how to solve?

In order not to affect the query, we can let the creation of the index run in the background.

Db.books.ensureIndex ({number:1},{true})

10. Delete Index

10.1. Batch and exact deletion index

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

Db.runcommand ({dropindexes: "Books", Index: "*"}) bulk Delete index

Iii. Spatial Index

11, MongoDB provides a strong spatial index, you can query a certain range of geographical coordinates. Examples are as follows:

Prepare data map.txt, as shown below:

var map = [{

"GIS": {

"X": 185,

"Y": 150

}

},{

"GIS": {

"X": 70,

"Y": 180

}

},{

"GIS": {

"X": 75,

"Y": 180

}

},{

"GIS": {

"X": 185,

"Y": 185

}

},{

"GIS": {

"X": 65,

"Y": 185

}

},{

"GIS": {

"X": 50,

"Y": 50

}

},{

"GIS": {

"X": 50,

"Y": 50

}

},{

"GIS": {

"X": 60,

"Y": 55

}

},{

"GIS": {

"X": 65,

"Y": 80

}

},{

"GIS": {

"X": 55,

"Y": 80

}

},{

"GIS": {

"X": 0,

"Y": 0

}

},{

"GIS": {

"X": 0,

"Y": 200

}

},{

"GIS": {

"X": 200,

"Y": 0

}

},{

"GIS": {

"X": 200,

"Y": 200

}

}]

for (var i = 0;i

Db.map.insert (Map[i])

}

First, add the 2D index (the default is to create a 2D index between [-180,180])

Db.map.ensureIndex ({"GIS": "2d"},{min:-1,max:201})

11.1, query the distance point (70,180) the nearest 3 points

Db.map.find ({"GIS": {$near: [70,180]}},{gis:1,_id:0}). Limit (3)//$near operator represents the center point; if limit is not specified, the default value is 100.

11.2. Query all points in a square with dots (50,50) and (190,190) diagonal

var box=[[50,50],[190,190]];//defines a rectangular region

Db.map.find ({"GIS": {"$within": {"$box": box}}},{gis:1,_id:0})//$box Rectangle Lookup

11.3, the query out to the center of the (55,80), radius of 50, under the rule of the center of the point in the area

var center=[55,80];//definition Center Point

var radius=50;//defines radius

Db.map.find ({"GIS": {"$within": {"$center": [Center,radius]}}});//$center Circle Lookup (Note that this is an array pass)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.