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

问题描述 投票:0回答:1

是否可以在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
)
mongodb kotlin spring-data-mongodb mongodb-indexes
1个回答
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" : {
        ...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.