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

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.

主站蜘蛛池模板: 炎陵县| 黔西县| 平邑县| 泰顺县| 临澧县| 莱芜市| 翼城县| 迭部县| 三河市| 永和县| 宜宾市| 延津县| 辽阳市| 本溪| 南充市| 徐汇区| 且末县| 罗平县| 休宁县| 福贡县| 台州市| 大冶市| 睢宁县| 南平市| 怀集县| 且末县| 朔州市| 韶山市| 大邑县| 霍城县| 阿勒泰市| 和顺县| 博客| 康保县| 长丰县| 滁州市| 大足县| 乌鲁木齐县| 霍林郭勒市| 正镶白旗| 淳安县|