- 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
}
推薦閱讀
- C程序設計簡明教程(第二版)
- Learning Cython Programming
- 劍指JVM:虛擬機實踐與性能調優(yōu)
- Rake Task Management Essentials
- Learning RabbitMQ
- 精通API架構:設計、運維與演進
- Learn WebAssembly
- Elasticsearch Server(Third Edition)
- Python Web數(shù)據(jù)分析可視化:基于Django框架的開發(fā)實戰(zhàn)
- 學習正則表達式
- PySide 6/PyQt 6快速開發(fā)與實戰(zhàn)
- Salesforce Reporting and Dashboards
- HTML5+CSS3 Web前端開發(fā)技術(第2版)
- Terraform:多云、混合云環(huán)境下實現(xiàn)基礎設施即代碼(第2版)
- AIRIOT物聯(lián)網(wǎng)平臺開發(fā)框架應用與實戰(zhàn)