为什么字段上的索引会根据 MongoDB 中的升序或降序而具有不同的大小

Why would the index on a field be a different size depending on if it is ascending or descending in MongoDB

我的集合中的 "created" 字段有两个单独的索引。一个索引按升序排序,另一个按降序排序。降序排序的索引大于升序排序的索引。创建的字段包含一个 Javascript 日期对象。什么会导致这种情况?

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

这里是 collection.getIndexes() 的详细信息。唯一的区别是降序索引是在后台创建的。

{
    "v" : 1,
    "key" : {
        "created" : 1
    },
    "name" : "created_1",
    "ns" : "Production.accounts"
},
{
    "v" : 1,
    "key" : {
        "created" : -1
    },
    "name" : "created_-1",
    "ns" : "Production.accounts",
    "background" : true
}

区别是前台创建升序索引,后台创建降序索引。

来自 the docs 关于后台索引创建:

Background index builds take longer to complete and result in an index that is initially larger, or less compact, than an index built in the foreground. Over time, the compactness of indexes built in the background will approach foreground-built indexes.

因此,如果您希望尽可能紧凑,请在前台创建索引,但随着时间的推移,后台索引也会变得更加紧凑。