MongoDB索引方向1对比true

时间:2015-03-01 20:41:31

标签: mongodb indexing

这些索引是多余的吗?

"1" : {
    "v" : 1,
    "key" : {
        "thing-id" : 1
    },
    "name" : "action-id_1",
    "ns" : "mydatabase.things"
},
"2" : {
    "v" : 1,
    "key" : {
        "thing-id" : true
    },
    "name" : "action-id_",
    "ns" : "mydatabase.things"
},

如果没有,1和真有什么区别?

1 个答案:

答案 0 :(得分:1)

没有区别 - 在这种情况下,true与1的作用相同。您可以按如下方式测试:

> db.test.drop()
> db.test.insert({ "a" : 1 })
> db.test.insert({ "a" : 2 })
> db.test.ensureIndex({ "a" : true })
> db.test.find().sort({ "a" : 1 }).explain().cursor
BtreeCursor a_true
> db.test.find().sort({ "a" : -1 }).explain().cursor
BtreeCursor a_true reverse

由于索引在用于按降序排序时是反向迭代的,因此它是一个升序索引。

您可以在索引规范中使用各种愚蠢的值:

> db.test.ensureIndex({ "a" : null })
> db.test.ensureIndex({ "a" : MaxKey })
> db.test.ensureIndex({ "a" : 0 })
> db.test.ensureIndex({ "a" : /^yogurt$/i })

您必须测试这些索引中的哪种索引 - 它不会出错并说它重复索引,因为索引会有不同的名称。他们很可能全都提升为" truthy"值。相关的问题似乎是SERVER-769,显然还没有被解决。

重点是,使用1或-1或字符串选项,如" text"。