- MongoDB Administrator’s Guide
- Cyrus Dasadia
- 310字
- 2021-07-02 15:47:48
How to do it...
- We begin by connecting to the mongo shell of the server and viewing all indexes on the system:
> db.mockdata.getIndexes()
The following result is obtained:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.mockdata"
},
{
"v" : 2,
"key" : {
"city" : 1,
"first_name" : 1
},
"name" : "city_1_first_name_1",
"ns" : "mydb.mockdata"
}
]
- Execute a dropIndex() command to delete a particular index:
> db.mockdata.dropIndex('city_1_first_name_1')
You should see the following result:
{ "nIndexesWas" : 2, "ok" : 1 }
- Let's recreate the index:
> db.mockdata.createIndex({'city':1}, {name: 'city_index'})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
- Run getIndexes() to fetch all indexes of the collection:
> db.mockdata.getIndexes()
We should see the following result:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.mockdata"
},
{
"v" : 2,
"key" : {
"city" : 1
},
"name" : "city_index",
"ns" : "mydb.mockdata"
}
]
- Try creating the index again on the city field:
> db.mockdata.createIndex({'city':1})
You should see the following message:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
- Check the size of the index:
stats = db.mockdata.stats()
stats["totalIndexSize"]
It should show the following result:
1818624
- Let us view the size of each index:
stats["indexSizes"]
This should show the following result:
{ "_id_" : 905216, "city_index" : 913408 }
- Re-index city_index:
> db.mockdata.reIndex('city_index')
The following result is obtained:
{
"nIndexesWas" : 2,
"nIndexes" : 2,
"indexes" : [
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "mydb.mockdata"
},
{
"v" : 2,
"key" : {
"city" : 1
},
"name" : "city_index",
"ns" : "mydb.mockdata"
}
],
"ok" : 1
}
推薦閱讀
- Mobile Web Performance Optimization
- PostgreSQL for Data Architects
- Vue.js 3.0源碼解析(微課視頻版)
- Oracle Database 12c Security Cookbook
- C語言程序設計教程
- 精通MATLAB(第3版)
- 利用Python進行數據分析
- iOS自動化測試實戰:基于Appium、Python與Pytest
- INSTANT Adobe Edge Inspect Starter
- Python 3.7從入門到精通(視頻教學版)
- Python預測之美:數據分析與算法實戰(雙色)
- C Primer Plus(第6版)中文版【最新修訂版】
- Learning Puppet
- Python GUI設計tkinter菜鳥編程(增強版)
- R High Performance Programming