我有以下架构:
{
name: String,
phones: [
{
number: String,
type: String
}
]
}
如何为 phones.number
编制索引以便我可以编写如下内容:
collection.aggregate([{
"$search":{
"compound":{
"should":[
{"autocomplete":{"query":"012345","path":"name"}},
{"autocomplete":{"query":"012345","path":"phones.number"}}
]
}
}
}])
文档 here给出一个字符串数组而不是对象数组的例子。
最佳答案
根据 this answer , 支持按数组中子文档的属性进行索引。只需通过 phones.number
创建一个索引。
参见 the documentation获取更多信息。
编辑
我混淆了标准索引和 Atlas Search 索引。从文档中,您应该能够以这种方式索引文档数组:
{
"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"
}
}
}
}
}
}
关于mongodb - 图集搜索对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65532707/