All posts

How to Query Firestore Using Natural Language

Firestore's query API is deliberate and structured — but writing compound where clauses, orderBy chains, and pagination logic for every analysis question adds friction for developers and makes self-service data exploration impossible for non-technical teammates.

Natural language querying lets you describe what you want in plain English and have the Firestore query generated automatically. This guide shows how it works with Insight O' Mate — a privacy-first AI database assistant for NoSQL databases.


What is natural language Firestore querying?

It's the process of translating a plain-English question into a Firestore query using an AI model. For example:

"Find all users who signed up this month and have not completed their profile."

becomes:

const startOfMonth = new Date();
startOfMonth.setDate(1);
startOfMonth.setHours(0, 0, 0, 0);

const snapshot = await db
  .collection("users")
  .where("createdAt", ">=", startOfMonth)
  .where("profileComplete", "==", false)
  .orderBy("createdAt", "desc")
  .get();

You describe the result you want. The AI generates the correct Firestore SDK call.


How the translation works

1. Collection detection. Insight O' Mate reads your Firestore collection names and document field names using the Firebase Admin SDK. Your document values are never read.

2. Query generation. Your prompt and the collection/field schema are sent to a stateless NLP engine. The engine produces a Firestore query in Firebase Admin SDK syntax.

3. Local execution. The query runs locally on your machine via the Admin SDK. The results are returned directly to the Insight O' Mate desktop app — no Firestore data passes through any Insight O' Mate server.


Example queries and generated Firestore code

Compound filter query

Prompt: Show me all active subscriptions that expire in the next 14 days.

const now = new Date();
const in14Days = new Date();
in14Days.setDate(now.getDate() + 14);

const snapshot = await db
  .collection("subscriptions")
  .where("status", "==", "active")
  .where("expiresAt", ">=", now)
  .where("expiresAt", "<=", in14Days)
  .orderBy("expiresAt", "asc")
  .get();

Array contains filter

Prompt: Find all blog posts tagged with "tutorial".

const snapshot = await db
  .collection("posts")
  .where("tags", "array-contains", "tutorial")
  .where("published", "==", true)
  .orderBy("publishedAt", "desc")
  .limit(20)
  .get();

Aggregate count

Prompt: How many support tickets are currently open?

const snapshot = await db
  .collection("tickets")
  .where("status", "==", "open")
  .count()
  .get();

const totalOpen = snapshot.data().count;

Subcollection query

Prompt: Get all messages in conversation conv-456 from the last 24 hours.

const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);

const snapshot = await db
  .collection("conversations")
  .doc("conv-456")
  .collection("messages")
  .where("sentAt", ">=", twentyFourHoursAgo)
  .orderBy("sentAt", "asc")
  .get();

Supported Firestore operations

OperationDescription
whereFilter by field equality, range, array-contains, in, not-in
orderBySort by one or more fields
limit / limitToLastPage size control
startAfter / endBeforeCursor-based pagination
count()Aggregate document count (requires Firestore Count Aggregation)
Subcollection queriesQueries against nested collections

What it does not generate: write operations (set, add, update, delete). This prevents accidental data mutations.


Privacy and security

Insight O' Mate connects to Firestore using your Firebase service account credentials:

  • Your service account JSON is stored in your OS keychain — never transmitted
  • Your document values never leave your machine
  • Only your prompt and collection/field names are sent to the NLP engine
  • The NLP engine is stateless and does not log your prompts

Setting up Firestore with Insight O' Mate

  1. Download Insight O' Mate
  2. Go to Firebase Console → Project Settings → Service Accounts
  3. Generate a new private key (downloads a JSON file)
  4. In the app, go to Connections → Add Firestore → upload the JSON file
  5. The app reads your collection structure
  6. Type your first question

Firestore query tips

  • Specify the collection. "Show me users who..." works better when your collection is named users.
  • Use time references. "Last 30 days", "this month", "today" all work.
  • Inspect before running. You can see the generated Firestore query before executing it.
  • Composite indexes. If Firestore returns an index error, the error message contains a link to create the index — this is a Firestore limitation, not Insight O' Mate.