MongoDB (iii)

Source: Internet
Author: User
Tags mongodb time 0

Index details
Index management
Spatial index

 for (var i = 0; i<200000; i++) {    Db.books.insert ({number:i,name:i+ "book"})}

1. Create a simple index

Data Preparation Index.js
1.1. Check the query performance first
var start = new Date ()
Db.books.find ({number:65871})
var end = new Date ()
End-start
1.2. Create an index for number
Db.books.ensureIndex ({number:1})
1.3. Execute the first code to see an order of magnitude performance gains


2. Where to pay attention to index use
2.1. When creating an index note that 1 is a positive order to create an index-1 is the reverse index
2.2. The creation of an index can affect the performance of the insert in a colleague who improves query performance
For documents that are frequently queried for less insertions, consider using an index
2.3. Conform to the index to be aware of the order of the index
2.4. The full index of each key does not necessarily improve performance.
Index is not a panacea
2.5. When doing sort work, if it's a large amount of data, you can also consider adding an index
To improve the performance of sorting

3. Name of the index
3.1 View index names with Vue


3.2 Creating an index specifying the name of the index at the same time
Db.books.ensureIndex ({name:-1},{name: "BookName"})

4. Unique index
4.1 How to troubleshoot a document books cannot insert duplicate values
Create a unique index
Db.books.ensureIndex ({name:-1},{unique:true})
Test
Db.books. Insert ({name: "1book"})


5. Kick Out duplicate values
5.1 If a unique index is recommended, there is already a duplicate value to handle
Db.books.ensureIndex ({name:-1},{unique:true,dropdups:true})
6.Hint
6.1 How do I force a query to use the 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 the index and query data using the query in detail
Db.books.find ({name: "1book"}). Explain ()


"Cursor": "Btreecursor name_-1" using the index
"nscanned": 1 to find several documents
"Millis": 0 query time 0 is very good performance

1.system.indexes
1.1 Checking the database for an established index in the shell
Db.system.indexes.find ()
Db.system.namespaces.find ()
2. Background execution
2.1 Performing the process of creating an index temporarily locks the table how is the problem solved?
In order not to affect the query we can call the index creation process in the background
Db.books.ensureIndex ({name:-1},{background:true})
3. Deleting an index
3.1 Batch and exact delete indexes
Db.runcommand ({dropindexes: "Books", Index: "Name_-1"})
Db.runcommand ({dropindexes: "Books", Index: "*"})

1.mongoDB provides a powerful spatial index that can query a range of geographic coordinates. See Example
Preparing Data Map.json

varMap = [{  "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(vari = 0;i<map.length;i++) {Db.map.insert (map[i])}
View Code

1. Check out the nearest 3 points from the distance point (70,180)
Add 2D Index
Db.map.ensureIndex ({"GIS": "2d"},{min:-1,max:201})
The default is to create a 2D index between [-180,180]
Query Point (70,180) the last 3 points
Db.map.find ({"GIS": {$near: [70,180]}},{gis:1,_id:0}). Limit (3)
2. Query all points in a square with points (50,50) and dots (190,190) diagonally
Db.map.find ({gis:{"$within": {$box: [[50,50],[190,190]]}}},{_id:0,gis:1})
3. Query for points in the center area with the center of the Circle (56,80) Radius of the 50 rule
Db.map.find ({gis:{$within: {$center: [[56,80],50]}}},{_id:0,gis:1}]

MongoDB (iii)

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.