- 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
}
推薦閱讀
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- 黑客攻防從入門到精通(實戰秘笈版)
- 演進式架構(原書第2版)
- Object-Oriented JavaScript(Second Edition)
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- C語言程序設計
- Python High Performance Programming
- Learning Docker Networking
- SQL Server 入門很輕松(微課超值版)
- Photoshop CC移動UI設計案例教程(全彩慕課版·第2版)
- WebStorm Essentials
- Python全棧開發:基礎入門
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- 前端架構設計
- Mastering Machine Learning with scikit-learn