Search with MongoDB Atlas Search made easy

Search-like functionalities are very popular these days and they are present almost everywhere on the internet. When implementing one, there are multiple things to consider, but that does not mean that this should be a hard process. Using MongoDB Atlas Search makes the process very straightforward and fast because you have everything out of the box!

Hmm.. everything is too abstract? Let’s see a practical example.

mongo-shell

We have a collection of restaurants and we retrieved those starting with “riv”. Simple enough, right?

IMPORTING THE DATABASE

If you want to follow along and use the exact same database as me, you can. I am using the MongoDB sample data package. You can either run this command in your Atlas CLI

> atlas clusters loadSampleData <clusterName> [options]

or just follow the MongoDB guide if you want to use a graphic interface.

CREATING THE SEARCH INDEX

In order to use the search capabilities of the MongoDB Atlas Search engine we are required to create a Search Index. Don’t worry, as I said, this is a very straightforward process.

The easiest way is to use the MongoDB Atlas interface. You can log into your MongoDB account and you should end up here. After that, open your desired cluster and select Search -> Create Search Index -> JSON Editor.

mongo-atlas

Your page should look like this. Here you have to select a database, a collection and a name for your index. And you can use the configuration provided by me which creates an index on the field name, of type autocomplete.

And that’s it! The search is implemented.

QUERY DATA USING THE SEARCH INDEX

As a quick recap, there are 2 ways to query data in MongoDB, one of them is using Mongo Query Language (MQL), and the other one is using aggregation pipeline. The searching index enforces the usage of aggregation pipeline, as it does not support MQL. Another quick thing to note here, when using aggregation pipeline only the first stage of the pipeline can use indexes. In our case $search stage will have to be the first stage in the pipeline, otherwise it will not have access to the index we created in the previous step.

If you are using MongoDB compass you can go to “Aggregations” section and query your data like this:

mongo-compass

You will have to input the name of the index, the text you want to search on, and the field that you want to make the search on.

To reach the output defined at the beginning, this is how you would write the query in Mongo Shell.

> getRestaurants = ({ startingWith }) => {
>   const searchStage = {
>     $search: {
>       index: 'restaurants',
>       autocomplete: {
>         query: 'riv',
>         path: 'name'
>       }
>     }
>   };
> 
>   const projectStage = {
>     $project: {
>       name: 1
>     }
>   };
> 
>   const limitStage = {
>     $limit: 3
>   };
> 
>   return db.restaurants.aggregate([searchStage, projectStage, limitStage]);
> };

Mongo Shell is also a JavaScript interpreter so you can declare functions (and call them) in the same way as in JavaScript. That’s why using some backend technology like Node.js or Deno together with MongoDB is really cool, one programming language for your entire codebase.

NEXT STEPS

We did it!

That’s how you would implement the search functionality in MongoDB. Keep in mind that, Atlas Search is a full-text search engine and in order to know all the possible customizations, you can check the official documentation. Another thing is that we discussed one of the possible approaches when designing a search in Mongo. There are more, but this one is what official MongoDB documentation recommends. This is a list of other approaches you can use and I am linking some documentation just in case you want to dive deeper:
⦁ using text index
⦁ using $regex operator

 

Andrei Paul, Certified MongoDB Developer

Previous post Next post

Comments are closed.