Home:ALL Converter>How to Create MongoDB Atlas Search Index on a Nested String

How to Create MongoDB Atlas Search Index on a Nested String

Ask Time:2022-10-28T18:37:48         Author:ololo

Json Formatter

I'm trying to create an Autocomplete UI for my frontend. As a result I'm running the following query:

    return Product.find({ 'data.text': { '$regex': `${searchTerm}`, '$options': 'i' } }).limit(200);

Unfortunately, the above query is so so slow.

After digging around, I was advised to use MongoDb Atlas Search Index to speed up my queries.

My product schema is of the form

{
  ...,
  data: Object //Can contain any key value pair one of which is text as used above
}

Creating a regular Atlas Search Index has been straightforward like

{
  "mappings": {
    "dynamic": false,
    "fields":[
      "name":{
        type:"string"
     }
    ]
  }
}

But how can I create a nested Search Index for my above use case. I would like to index 'data.text' only

Author:ololo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74234151/how-to-create-mongodb-atlas-search-index-on-a-nested-string
yy