MongoDB-我可以同时在一个字段上使用@TextIndexed和@Indexed吗?

时间:2020-02-19 09:50:05

标签: mongodb kotlin spring-data-mongodb mongodb-indexes

MongoDB是否可以在同一列上同时具有索引和文本索引?

我想按国家/地区代码(即“美国”)查询Question集合,并按国家/地区代码将Country集合中的相关数据作为ID展开。

Spring Data MongoDB / Kotlin的示例代码:

@Document
data class Question(
    @Id val id: String,

    @TextIndexed
    @Indexed(name = "question_country_code_index")
    val countryCode: String
)

1 个答案:

答案 0 :(得分:0)

可以将两个注释都应用到完全相同的属性。 IndexResolver然后将创建两个索引。

{
    "v" : 2,
    "key" : {
        "_fts" : "text",
        "_ftsx" : 1
    },
    "name" : "Question_TextIndex",
    "ns" : "db.question",
    "weights" : {
        "textIndexedPropertyWithDefaultWeight" : 1
    },
    "language_override" : "language",
    "textIndexVersion" : 3
},
{
    "v" : 2,
    "key" : {
        "countryCode" : 1
    },
    "name" : "question_country_code_index",
    "ns" : "db.question",
    "collation" : {
        ...
    }
}
相关问题