All posts

How to Query MongoDB Using Natural Language

Writing MongoDB queries by hand is powerful — but it can slow you down. Aggregation pipelines with $match, $group, $lookup, and $project are expressive, but they take time to compose, especially for one-off analysis questions.

Natural language querying lets you ask a question in plain English and have the database query generated automatically. This guide explains how it works, what it can handle, and how to do it with Insight O' Mate — a privacy-first AI database assistant that runs locally.


What is natural language MongoDB querying?

Natural language querying is the process of converting a plain-English question into a database query. For MongoDB, that means translating a sentence like:

"Show me the top 10 customers by total revenue this month"

into a MongoDB aggregation pipeline:

db.orders.aggregate([
  {
    $match: {
      createdAt: {
        $gte: new Date(new Date().getFullYear(), new Date().getMonth(), 1)
      }
    }
  },
  {
    $group: {
      _id: "$customerId",
      totalRevenue: { $sum: "$amount" }
    }
  },
  { $sort: { totalRevenue: -1 } },
  { $limit: 10 }
])

The key insight: you describe the result you want, and the AI generates the correct MQL.


How the translation works

Insight O' Mate uses a three-step process:

1. Schema inference. The tool reads your collection names and field names (never your documents or field values). It uses this schema to understand what data is available.

2. Query generation. Your prompt and the schema are sent to a stateless NLP engine. The engine produces a MongoDB query as structured JSON.

3. Local execution. The query runs on your machine, directly against your MongoDB instance. No data is sent to any server.


Example queries and generated MongoDB code

Finding documents with conditions

Prompt: Find all orders with status "failed" in the last 7 days.

db.orders.find({
  status: "failed",
  createdAt: {
    $gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
  }
})

Aggregation with grouping

Prompt: What is the average order value per product category this quarter?

db.orders.aggregate([
  {
    $match: {
      createdAt: { $gte: new Date(new Date().getFullYear(), Math.floor(new Date().getMonth() / 3) * 3, 1) }
    }
  },
  {
    $lookup: {
      from: "products",
      localField: "productId",
      foreignField: "_id",
      as: "product"
    }
  },
  { $unwind: "$product" },
  {
    $group: {
      _id: "$product.category",
      avgOrderValue: { $avg: "$amount" },
      count: { $sum: 1 }
    }
  },
  { $sort: { avgOrderValue: -1 } }
])

Simple count

Prompt: How many users signed up today?

db.users.countDocuments({
  createdAt: {
    $gte: new Date(new Date().setHours(0, 0, 0, 0))
  }
})

What MongoDB operations does Insight O' Mate support?

OperationDescription
findBasic document retrieval with filters
aggregatePipelines with $match, $group, $sort, $limit, $lookup, $project, $unwind
countDocumentsCount matching documents
distinctUnique field values

What it does not generate: write operations (insertOne, updateOne, deleteOne). This protects you from accidental mutations.


Privacy and security

When you use Insight O' Mate:

  • Your MongoDB connection string is stored in your OS keychain — not transmitted anywhere
  • Your documents and field values never leave your machine
  • Only your prompt and field/collection names are sent to the NLP engine
  • The NLP engine is stateless — it does not log your prompts

You can verify this with a network monitor — the only outbound request is to the Insight O' Mate NLP engine, and it contains no document data.


Setting up MongoDB with Insight O' Mate

  1. Download Insight O' Mate
  2. Open the app and go to Connections
  3. Paste your MongoDB connection string (e.g. mongodb+srv://user:pass@cluster.mongodb.net/mydb)
  4. The app will read your collection and field names
  5. Type your first question in the query input

It works with MongoDB Atlas, self-hosted replica sets, and standalone MongoDB instances.


Tips for better MongoDB queries

  • Be specific about time ranges. "Last 7 days" works better than "recently."
  • Name your collections clearly. Insight O' Mate reads your schema, so orders is more useful than data.
  • Inspect the generated query. You can always see the full MQL before running it.
  • Iterate. Ask a follow-up question to refine results.