- 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
}
推薦閱讀
- Python測試開發入門與實踐
- Modern JavaScript Applications
- Apache Spark 2.x for Java Developers
- Learning Concurrency in Kotlin
- Python機器學習:預測分析核心算法
- Java零基礎實戰
- Python自然語言理解:自然語言理解系統開發與應用實戰
- C++ System Programming Cookbook
- Visual Basic語言程序設計基礎(第3版)
- Python面向對象編程(第4版)
- Spring Boot 2+Thymeleaf企業應用實戰
- Java程序設計(項目教學版)
- Implementing Splunk(Second Edition)
- Scratch超人漫游記:創意程序設計:STEAM創新教育指南
- Python 3.8編程快速入門