Why the index in the field will be of a different size, depending on whether it will increase or decrease in MongoDB

I have two separate indexes for the "created" field in my collection. One index is sorted in ascending order, and the other sorted in descending order. An index sorted in descending order is larger than an index sorted in ascending order. The created field contains a Date Javascript object. What can cause this?

"indexSizes" : { "_id_" : 212862160, "created_1" : 136424736, "created_-1" : 252376768 }, 

Here are the details from the .getIndexes () collection. The only difference is that the descending index was created in the background.

 { "v" : 1, "key" : { "created" : 1 }, "name" : "created_1", "ns" : "Production.accounts" }, { "v" : 1, "key" : { "created" : -1 }, "name" : "created_-1", "ns" : "Production.accounts", "background" : true } 
+5
source share
1 answer

The difference is that the upstream index is created in the foreground, and the downward index is created in the background.

From documents to create a background index:

Collecting background indexes takes longer and results in an index that is initially more or less compact than the index built in the foreground. Over time, the compactness of indexes built in the background will approach the built-in indexes.

So, create your index in the foreground if you want it to be as compact as possible, but over time, the background index will also become more compact.

+2
source

All Articles