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

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.

主站蜘蛛池模板: 樟树市| 牡丹江市| 宜昌市| 陆丰市| 景泰县| 甘孜县| 精河县| 肥东县| 丹寨县| 河北区| 凤台县| 旬阳县| 塔河县| 汝州市| 林西县| 兰西县| 建德市| 福州市| 娄底市| 武穴市| 天台县| 辽阳市| 黔西| 南郑县| 黄平县| 开封县| 大同市| 云龙县| 湖南省| 巴彦淖尔市| 手游| 南华县| 老河口市| 余姚市| 新巴尔虎右旗| 来安县| 德钦县| 盱眙县| 江口县| 卢湾区| 津市市|