Your First AI Query

Now that you're connected to MongoDB, let's try your first AI-powered query! Agent M's AI chat interface makes it easy to build MongoDB queries using natural language.

How AI Queries Work

The magic of Agent M lies in its ability to translate your natural language into MongoDB queries. Instead of memorizing complex syntax, you simply describe what you want in plain English. For example, when you say "Show me all users from New York," the AI creates the proper MongoDB query db.users.find({city: "New York"}) and displays the results in a beautiful, easy-to-understand interface.

Starting Your First Query

1. Open the AI Chat

Once connected to a database, you'll see the main chat interface with a chat input at the bottom, a database tree on the left showing your collections, and the query results area in the center where your data will appear.

2. Type Your Request

In the chat input, simply describe what you want to do in natural language. You might ask to "show me all documents in the users collection," "find products with price greater than 100," "count how many orders were placed last month," or "get the top 10 most popular products." The AI understands all these different ways of asking for data.

3. Review the Generated Query

The AI will generate the appropriate MongoDB query, show you the exact syntax it's using, and explain what the query does. This transparency helps you learn MongoDB syntax over time while giving you confidence in what the AI is doing. The AI will ask for your confirmation before running the query, so you're always in control.

4. Execute and View Results

Once you approve the query, it runs against your database and the results appear in multiple view modes. You can save interesting queries to your favorites for quick access later, and everything gets automatically added to your query history so you can easily find and re-run queries.

Example: First Query Walkthrough

Let's say you want to see all users in your database:

  1. 1
    Type: "Show me all users"
  2. 2
    AI generates: db.users.find()
  3. 3
    AI explains: "This query will return all documents from the users collection"
  4. 4
    You click: "Execute Query"
  5. 5
    Results appear: In table, tree, or JSON view

Understanding Query Results

Agent M gives you multiple ways to view your data, each optimized for different use cases.

The Table View provides a clean, spreadsheet-like display that's easy to scan and read, with sortable columns that make it perfect for most data analysis tasks.

The Tree View shows the hierarchical structure of your documents, with expandable nested objects that help you understand complex document relationships. This is particularly useful when working with deeply nested data structures.

For developers, the JSON View displays the raw MongoDB document format with proper formatting, making it easy to copy and paste into other tools. The Raw View shows unformatted JSON output, which is useful for debugging or when you need to see the exact data structure without any formatting.

Tips for Better AI Queries

The key to getting great results from Agent M is being specific about what you want. Instead of saying "show me users," try "show me users who registered in the last 30 days." The more specific you are, the better the AI can help you find exactly what you're looking for.

When working with multiple collections, it helps to mention the collection name. Rather than just saying "find products," specify "find products in the inventory collection" to avoid any ambiguity about which collection you're interested in.

For large datasets, it's always a good idea to specify limits. Instead of asking for "all orders," try "get the 10 most recent orders" to avoid overwhelming yourself with too much data. You can always ask for more if you need it.

Use filters to narrow down your results. Instead of a broad "show me users," try "show me users with status 'active' and age over 18" to get exactly the subset of data you're interested in.

Common Query Patterns

Finding Documents

"Find all users with email containing '@gmail.com'"
"Show me products with category 'electronics'"
"Get documents where price is between 50 and 100"

Counting and Aggregation

"How many users are there?"
"Count products by category"
"What's the average price of electronics?"

Sorting and Limiting

"Show me the 5 most expensive products"
"Get the newest 10 blog posts"
"Find the oldest user account"

Next Steps