Mik*_*ika 4 mongodb mongodb-atlas mongodb-atlas-search
我有以下架构:
{
name: String,
phones: [
{
number: String,
type: String
}
]
}
Run Code Online (Sandbox Code Playgroud)
我如何索引phones.number以便我可以编写类似以下内容的内容:
collection.aggregate([{
"$search":{
"compound":{
"should":[
{"autocomplete":{"query":"012345","path":"name"}},
{"autocomplete":{"query":"012345","path":"phones.number"}}
]
}
}
}])
Run Code Online (Sandbox Code Playgroud)
这里的文档给出了字符串数组的示例,但没有提供对象数组的示例。
根据此答案,支持按数组中子文档的属性进行索引。只需创建一个索引即可phones.number。
请参阅文档以获取更多信息。
编辑
我混淆了标准索引和 Atlas 搜索索引。从文档中,您应该能够以这种方式索引文档数组:
{
"analyzer":"lucene.standard",
"searchAnalyzer":"lucene.standard",
"mappings":{
"dynamic":false,
"fields":{
"name":{
"type":"string",
"analyzer":"lucene.standard"
},
"phones":{
"type":"document",
"fields":{
"number":{
"type":"string",
"analyzer":"lucene.standard"
},
"type":{
"type":"string",
"analyzer":"lucene.standard"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)