Atlas 搜索对象数组

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)

这里的文档给出了字符串数组的示例,但没有提供对象数组的示例。

Chr*_*ris 5

根据此答案,支持按数组中子文档的属性进行索引。只需创建一个索引即可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)

  • 实际上,您所想的方式并不支持点表示法。用户需要将 Phones 字段指定为“document”类型及其相应的对象成员。请参阅此处有关索引定义的文档:https://docs.atlas.mongodb.com/reference/atlas-search/index-definitions#document (2认同)
  • @FirojSiddiki 可能有点晚了,但索引映射中的字段顺序在 mongodb atlas 索引中并不重要。 (2认同)