I have exactly the same problem. .find() works fine with and without params and .aggregate() works with a “$match” stage, but when using autocomplete it only returns an empty array.
@LudiG. Apologies for the delay in responding. Unfortunately I did not find a solution to utilise mongodb’s autocomplete feature. I ended up using another approach to address the issue I was facing. What are you trying to use the feature for? Best of luck.
@Eest_Said no worries!
I am still trying to figure out the error, but couldn’t track it down yet. At first I thought it’s an issue of mongoose returning plain javascript instead of mongoose objects when using aggregate (mongoose documentation), but it works fine with a $match stage so this shouldn’t be the problem.
I want to use it for suggestions in a text search. It’s pretty much a straight forward application scenario for autocomplete. What did you use instead?
The issue was that I used a different index name than “default” when creating the index and the autocomplete documentation (autocomplete) didn’t mention that this name has to be specified if it deviates from “default”. However I found a small note here in step 8, where it says that you need to specify the index name in the parameters of $search.
Hence, when specifying the index like this it worked:
So I used a simple regex to query the DB. My requirement is to allow searching of consecutive values. For example, the user might be searching for all entries that contain apples AND bananas so a search for ‘apples,b’ should return all entries that contain apples AND all entries starting with ‘b’. Because I use an array to store the progressive search, the key line of code to capture the current search value is …
newQueryArray.splice(-1, 1, new RegExp(^${currentQueryString}));
Then the DB is queried with a .find() using the $all operator.
let result = await FruitTest.find({
tags: {
// Filter must inclupde ALL array items.
// Final array item is regex - see newQueryArray above.
$all: newQueryArray,
},
});
Thanks again @LudiG and I will try an implement what you have suggested over the weekend.