MongoDB索引:对象键与字符串数组

时间:2016-02-02 01:25:51

标签: performance mongodb indexing mongoose schema

我是MongoDB的新手,一直在研究架构设计和索引。我知道你可以索引属性而不管它的值(ID,数组,子文档等等),但我不知道的是,索引字符串数组或嵌套对象是否有性能优势? #39; s键。

以下是我正在考虑的两种情景的例子(在Mongoose中):

// schema
mongoose.Schema({
    visibility: {
        usa: Boolean,
        europe: Boolean,
        other: Boolean
    }
});
// query
Model.find({"visibility.usa": true});

OR

// schema
mongoose.Schema({
    visibility: [String] // strings could be "usa", "europe", and/or "other"
});
// query
Model.find({visibility: "usa"});

文档可以有一个,两个或全部三个可见性选项。

此外,如果我使用布尔对象设计,我可以简单地索引可见性字段,还是需要在美国,欧洲和其他国家/地区设置索引?

1 个答案:

答案 0 :(得分:1)

在MongoDB中,在字符串数组上创建索引会导致multiKey索引,其中数组中的所有字符串形成索引键并指向同一个文档。因此,在您的情况下,它将与嵌套对象键相同。

如果你选择布尔设计,你可以将索引放在可见性字段上。你可以进一步阅读MongoDB Mulitkey indexing