MongoDB Compass — Convert Text into Queries with AI-Powered Natural Language
Published: 2024-03-12 05:12:08


**This is a tutorial for my [YouTube Video](https://www.youtube.com/watch?v=3Qtzxrp5iO0)**

In this tutorial, we’ll explore Query With Natural Language, a revolutionary feature in MongoDB Compass. This advanced functionality is ideal for users looking to employ AI for query generation based on text input. This hands-on approach will showcase why Query With Natural Language is not just a useful tool, but a necessary one for modern data management workflows. The article is structured as follows:

- Introduction — Use Cases, Concepts and Requirements
- Importing dataset with MongoDB Tools
- Prompt a Natural Language in Compass
- Conclusion

---


## Introduction — Use Cases, Concepts and Requirements

Today, we’re diving into a topic that was inspired by a recent conversation I had with friends. They expressed the challenges of transitioning from a relational database to MongoDB, struggling with creating queries and aggregation pipelines due to lack of experience. This got me thinking about the broader implications for those starting new roles in MongoDB-dependent companies.

As a result, I decided to explore a feature that addresses these issues: MongoDB Compass. By harnessing AI, MongoDB Compass automates query generation from your text input, revolutionizing the querying process. This feature, available from version 1.40.x and powered by Azure Open AI, ensures your data’s security, as it’s not stored on any third-party system or used for AI model training.

To get started, create your [free account on Atlas](https://www.mongodb.com/cloud/atlas/register) and install [MongoDB Compass](https://www.mongodb.com/try/download/compass) version 1.40 or later.

---


## Importing dataset with MongoDB Tool

> Notice: If you have already your data, you can skip ahead to the last topic.

I separated this section into three steps:

1. Creating an instance of MongoDB using Docker.
2. Downloading the dataset.
3. Importing the dataset using MongoDB tools.

**1 — Creating an instance of MongoDB using Docker**
First, we need to set up an instance of MongoDB. We will use Docker Compose for this task. Below is a docker-compose.yml file to create a MongoDB instance:

```
version: '3.1'

services:
  mongodb:
    image: mongo
    container_name: my_mongodb
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db

volumes:
  mongodb_data:
    driver: local
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eu5j1jl2voxzbkxcc8mj.png)

**2 — Downloading the dataset.**
Kaggle.com is an online platform that hosts data science competitions and offers datasets for exploration. [Download ](https://www.kaggle.com/datasets/fernandol/countries-of-the-world)the countries Json:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hw6fvbuh089xj76gxiu5.png)

important: When downloading the file, make sure it is in JSON format. If not, use a converter, such as [this one](https://csvjson.com/), to ensure success.

**3 — Importing the dataset using MongoDB tools.**
[MongoDB Tools](https://www.mongodb.com/try/download/database-tools) will help us to bring data into our database. Download it:



Perfect! Now it’s time to import our dataset. I’ve saved my dataset with the name countries.json. To import it, navigate to the MongoDB Tools Downloaded /bin folder run:

```
.\mongoimport mongodb://localhost:27017/countries_db --collection countries --jsonArray ./countries.json

```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/niml0fy5zaz3y7h509sh.png)

---


## Prompt a Natural Language in Compass

In this section, we will explore how to create queries based on the country’s information. We will cover finding countries with the highest and lowest populations, as well as grouping countries by region to calculate the average population in each region.

1. Countries with Highest Population: “Find the countries with the highest population, sorting the results in descending order by population.”
2. Grouping by Region: “Group the countries by region and calculate the average population in each region.”

First, click on Compass settings:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6g055936rm69golb1bsg.png)

Then feature preview, and enable natural language prompt. As you can see, I’m already logged into my Atlas account, so you’ll need to do the same.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6pd0r0w4x2taoeu8bu00.png)

Then, return to our collections. Here, you’ll find the “Generate Query” option. Click on that:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdf9pmw46wtlv9hc0uo1.png)

As you can observe, MongoDB will enable a field to enter with queries. To start, paste the following sentence, click in generate then Find:

> “Find the countries with the highest population, sorting the results in descending order by population.”


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1akg42vje1k1b1kx3yfe.png)

To conclude, let’s create some aggregation pipelines. Click on “Aggregations” and paste the following sentence:

> “Group the countries by region and calculate the average population in each region.”

This will demonstrate how MongoDB Compass can simplify the creation of stages using natural language queries.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wanjjs9a30jjvb6sa3xd.png)

---

## Conclusion
In conclusion, MongoDB Compass’s Query with Natural Language feature revolutionizes the way users interact with databases. By allowing users to input queries in plain language, MongoDB Compass simplifies the querying process, especially for those new to MongoDB or database queries in general.

I hope you have enjoyed this content. For those who might not be aware, I’m a member of the MongoDB Community Creator. For a deeper dive and detailed guidance, check out my video.

Do you like MongoDB? Check out my other articles:

- [Relational Migrator — From PostgreSQL to Atlas](https://itnext.io/mongodb-relational-migrator-e84c49220cef)
- [MongoDB Atlas — Chart](https://itnext.io/mongodb-atlas-charts-importing-json-file-and-crafting-powerful-visualizations-915e20759a89)
- [Improving MongoDB Performance | Indexes and Explain Plan in MongoDB Compass](https://medium.com/predict/mongodb-compass-optimizing-performance-with-indexes-and-explain-plan-3fc15914a4a7)
- [Understanding MongoDB Replication: A Step-by-Step Replica Set Creation.](https://itnext.io/understanding-mongodb-replication-a-step-by-step-tutorial-on-building-a-replica-set-cluster-b4267e4e2737)

I hope this has been informative and helpful. Until next time!

#MongoDB #Query #AI #NLP