1

I want an autocomplete search on a name field in my collection on MongoDB Atlas. I've configured a search index in MongoDB atlas as following:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "name": {
        "foldDiacritics": false,
        "maxGrams": 8,
        "type": "autocomplete"
      }
    }
  }
}

and I'm trying to search via mongoose a substring in the entire collection like this:

collection.aggregate([
    {
      $search: {
        autocomplete: {
          path: 'name',
          query: query,
        },
      },
    },
    {
      $limit: 10,
    },
    {
      $project: {
        _id: 0,
        name: 1,
      },
    },
  ]);

I always get 0 results, does anyone can help me understand what am I doing wrong?

Thanks, Erez

Doug
  • 14,387
  • 17
  • 74
  • 104
  • Can you provide a document that is in your collection and an example query that you ran? – Doug Feb 07 '21 at 19:14
  • Thanks for replying, I've finally figured the problem. My index name was not 'default' and when the default name is changed you should specify the index name in the search. I've changed the index name to 'default' and it works! – erez victor Feb 10 '21 at 20:41

1 Answers1

2

The problem was that I've changed the default index name (which is 'default') and in that case, I needed to specify the new index name in the query or change the index to be named 'default'. Didn't found any mention of that in the MongoDB documentation though.