Issue in MongoDB document search

Viewed 60

I am new to MongoDB. And I have the following issue on currently developing web application.

We have an application where we use mongoDB to store data.

And we have an API where we search for the document via text search.

As an example: if the user type “New York” then the request should send the all the available data in the collection to the keyword “New York". (Here we call the API for each letter typed.) We have nearly 200000 data in the DB. Once the user searches for a document then it returns nearly 4000 data for some keywords. We tried with limiting the data to 5 – so it returns the top 5 data, and not the other available data. And we tried without limiting data now it returns hundreds and thousands of data as I mentioned. And it causes the request to slow down.

At Frontend we Bind search results to a dropdown. (NextJs)

My question:

  • Is there an optimizing way to search a document?

  • Are there any suggestions of a suitable way that I can implement this requirement using mongoDB and net5.0?

  • Or any other Implementation methods regarding this requirement?

Following code segment shows the query to retrieve the data to the incomming keyword.

        var hotels = await _hotelsCollection
            .Find(Builders<HotelDocument>.Filter.Text(keyword))
            .Project<HotelDocument>(hotelFields)
            .ToListAsync();

        var terminals = await _terminalsCollection
            .Find(Builders<TerminalDocument>.Filter.Text(keyword))
            .Project<TerminalDocument>(terminalFeilds)
            .ToListAsync();

        var destinations = await _destinationsCollection
            .Find(Builders<DestinationDocument>.Filter.Text(keyword))
            .Project<DestinationDocument>(destinationFields)
            .ToListAsync();
1 Answers

So this is a classic "autocomplete" feature, there are some known best practices you should follow:

  1. On the client side you should use a debounce feature, this is a most. there is no reason to execute a request for each letter. This is most critical for an autocomplete feature.

On the backend things can get a bit more complicated, naturally you want to be using a db that is suited for this task, specifically MongoDB have a service called Atlas search that is a lucene based text search engine.

This will get you autocomplete support out of the box, however if you don't want to make big changes to your infra here are some suggestions:

  1. Make sure the field your searching on is indexed.
  2. I see your executing 3 separate requests, consider using something like Task.WhenAll to execute all of them at once instead of 1 by 1, I am not sure how the client side is built but if all 3 entities are shown in the same list then ideally you merge the labels into 1 collection so you could paginate the search properly.
  3. As mentioned in #2 you must add server side pagination, no search engine can exist without one. I can't give specifics on how you should implement it as you have 3 separate entities and this could potentially make pagination implementation harder, i'd consider wether or not you need all 3 of these in the same API route.
Related