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 < six >