图集搜索对象数组

Atlas search array of objects

我有以下架构:

{
   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"
          }
        }
      }
    }
  }
}