I have the following schema:
{
   name: String,
   phones: [
        {
            number: String,
            type: String
        }
   ]
}
How do I index phones.number so that I can write something like:
collection.aggregate([{
       "$search":{ 
            "compound":{
                  "should":[
                      {"autocomplete":{"query":"012345","path":"name"}},
                      {"autocomplete":{"query":"012345","path":"phones.number"}}
                  ]
             }
         }
}])
The docs here give an example for an array of strings but not an array of objects.
As per this answer, indexing by a property of a subdocument in an array is supported. Just create an index by phones.number.
See the documentation for more information.
EDIT
I was confusing standard indexing with indexing for Atlas Search. From the documentation, you should be able to index an array of documents this way:
{
  "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"
          }
        }
      }
    }
  }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With