DEV Community

Cover image for MongoDB Compass — Convert Text into Queries with AI-Powered Natural Language
Ricardo Mello
Ricardo Mello

Posted on

MongoDB Compass — Convert Text into Queries with AI-Powered Natural Language

This is a tutorial for my YouTube Video

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 and install MongoDB 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
Enter fullscreen mode Exit fullscreen mode

Image description

2 — Downloading the dataset.
Kaggle.com is an online platform that hosts data science competitions and offers datasets for exploration. Download the countries Json:

Image description

important: When downloading the file, make sure it is in JSON format. If not, use a converter, such as this one, to ensure success.

3 — Importing the dataset using MongoDB tools.
MongoDB 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

Enter fullscreen mode Exit fullscreen mode

Image description


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

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

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

Image description

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

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


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:

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

MongoDB #Query #AI #NLP

Top comments (0)