First add some data to the database and enter the following command:
for (Var i=1;i<10;i++) Db.customers.insert ({name: "Jordan" +i,country: "American"})
for (Var i=1;i<10;i++) Db.customers.insert ({name: "Gaga" +i,country: "American"})
for (Var i=1;i<10;i++) Db.customers.insert ({name: "Ham" +i,country: "UK"})
for (Var i=1;i<10;i++) Db.customers.insert ({name: "Brown" +i,country: "UK"})
for (Var i=1;i<10;i++) Db.customers.insert ({name: "Ramda" +i,country: "Malaysia"})
Use the following command to view that the current database does not exist in the index (except _id)
Db.system.indexes.find ()
Now add a column index to the Name field, index syntax:
Db.collection.ensureIndex (keys,options)
The keys is a document that contains the sort direction of the fields and indexes to increase the index; option is an optional parameter that controls the creation of the index love you way. The specific commands are as follows:
Db.customers.ensureIndex ({name:1})
{
"Createdcollectionautomatically": false,
"Numindexesbefore": 1,
"Numindexesafter": 2,
"OK": 1
}
Using Indexes.find (), you can query to the index you just built. If you create a unique index, you can use the following command:
Db.customers.ensureIndex ({name:1},{unique:true})
{
"Createdcollectionautomatically": false,
"Numindexesbefore": 1,
"Numindexesafter": 2,
"OK": 1
}
(This article uses a unique index)
To see if the data uses an index:
Db.customers.find ({name: "Ramda9"}). 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": [
[
"Ramda9",
"Ramda9"
]
]
},
"Server": "localhost.localdomain:27017",
"Filterset": false
}
The "Nscannedobjects" from the bold word indicates that the total number of documents scanned during the query is indexed. Now query the Country field again:
Db.customers.find (). Count ()
45
Db.customers.find ({country: "Malaysia"}). Explain ()
{
"Cursor": "Basiccursor",
"Ismultikey": false,
"N": 9,
"nscannedobjects":
"nscanned": 45,
"Nscannedobjectsallplans": 45,
"Nscannedallplans": 45,
"Scanandorder": false,
"IndexOnly": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 0,
"Server": "localhost.localdomain:27017",
"Filterset": false
}
It can be seen that this is using a full table scan and is not used to the index.
To establish a compliance index:
now create an index for country:
for (Var i=1;i<10;i++) Db.customers.insert ({name: "Lanbo" +i,country: "Malaysia"})
Query index Condition:
Db.customers.find ({country: "Malaysia"}). Explain ()
The result is a full table scan. Now try to create a normal index on the country:
Db.customers.ensureIndex ({country:1})
Re-execute the explain statement again:
Db.customers.find ({country: "Malaysia"}). Explain ()
{
"Cursor": "Btreecursor country_1",
"Ismultikey": false,
"N": 18,
"nscannedobjects":
"nscanned": 18,
"Nscannedobjectsallplans": 18,
"Nscannedallplans": 18,
"Scanandorder": false,
"IndexOnly": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 0,
"Indexbounds": {
"Country": [
[
"Malaysia",
"Malaysia"
]
]
},
"Server": "localhost.localdomain:27017",
"Filterset": false
}
The index is used and a query is made to 18 records. Now create a qualifying index:
Db.customers.ensureIndex ({name:1,coutry:1})
Db.customers.find ({name: "Lanbo2", Country: "Malaysia"}). Explain ()
{
" cursor": "Btreecursor name_1_coutry_1",
"Ismultikey": false,
"N": 1,
"Nscannedobjects": 1,
"Nscanned": 1,
"Nscannedobjectsallplans": 3,
"Nscannedallplans": 3,
"Scanandorder": false,
"IndexOnly": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 0,
"Indexbounds": {
"Name": [
[
"Lanbo2",
"Lanbo2"
]
],
"Coutry": [
[
{
"$minElement": 1
},
{
"$maxElement": 1
}
]
]
},
"Server": "localhost.localdomain:27017",
"Filterset": false
}
The match index for name and country is used here. (a unique index of name is removed, Db.customers.dropIndex ("Name_1"))
This article is from the "Technical Blog" blog, please be sure to keep this source http://raytech.blog.51cto.com/7602157/1692774
MongoDB Index Simple usage tips