官术网_书友最值得收藏!

How it works...

Most of the commands are pretty self-explanatory. In steps 1 and 2, we view and delete index respectively. You can also use db.<collection>.dropIndexes() to delete all indexes. In step 3, we recreate the index on the city field, but this time we provide an additional parameter to customize the name of the index. This can be confirmed by viewing the output of the getIndexes() command, as shown in step 3. Next, in step 4, we try to create another index on the city field (in ascending order). However, as we already have an index on this field, this would be redundant and hence MongoDB does not allow it. If you change the 1 to -1 that is, change the sort order to descending, then your operation would succeed and you'd end up with another index on the city field, but sorted in descending order.

In step 5, we run the stats() function on the collection which can alternately be run as db.mockdata.runCommand('collstats') and save its output in a temporary variable called stats. If we inspect the totalIndexSizeand indexSizes keys, we can find the total as well as index specific sizes, respectively. At this point, I would strongly suggest you have a look at other keys in the output. It should give you a peek into the low-level internals of how MongoDB manages each collection.

Lastly, in step 6, we re-index an existing index. In that, it drops the existing index and rebuilds it either in the foreground or background, depending on how it was set up initially. It is usually not necessary to rebuild the index, however, as per MongoDB's documentation you may choose to do so if you feel that the index size may be disproportionate or your collection has significantly grown in size.

主站蜘蛛池模板: 罗定市| 乐亭县| 高平市| 巴青县| 凉山| 五寨县| 依安县| 定远县| 中山市| 广汉市| 洛扎县| 名山县| 宁德市| 海伦市| 湘乡市| 香格里拉县| 和田县| 宜州市| 康马县| 富蕴县| 平安县| 雷州市| 五台县| 商洛市| 周至县| 巫山县| 哈巴河县| 科技| 武山县| 始兴县| 宜昌市| 沂源县| 噶尔县| 右玉县| 蒙自县| 远安县| 安阳县| 龙胜| 宿州市| 团风县| 阿荣旗|