Loading SQL to Firestore Converter...
Please wait a moment

From SQL Queries to Firestore Code

Paste a SQL statement above and you get code for Cloud Firestore: WHERE becomes where() filters with and() and or() where SQL needs them, ORDER BY and LIMIT become orderBy() and limit(), and writes become addDoc(), setDoc() and updateDoc(). Firestore isn't a SQL database, so wherever it behaves differently a note in the output tells you what to watch for.

How to Convert SQL to Firestore

Step 1

Paste Your SQL

Type or paste the statement, click "Upload" for a .sql or .txt file, or click "Sample" to see a SELECT with IN, a date and ORDER BY. Several statements separated by semicolons are converted one after another.

Example: a SELECT with AND, OR and LIMIT

SELECT name, email
FROM users
WHERE status = 'active'
  AND (plan = 'pro' OR trial = TRUE)
ORDER BY name
LIMIT 20;
Step 2

Read the Firestore Query

Because the WHERE clause mixes AND and OR, the filters go into one and() with an or() inside, the way Firestore wants composite filters. The column list turns into d.get() calls, since the web SDK always returns whole documents. Above the query (left out here) the output has its imports and a note that orderBy("name") skips documents without a name field.

Output for the example

const q = query(
  collection(db, "users"),
  and(
    where("status", "==", "active"),
    or(
      where("plan", "==", "pro"),
      where("trial", "==", true)
    )
  ),
  orderBy("name"),
  limit(20)
);
const snapshot = await getDocs(q);
const results = snapshot.docs.map((d) => ({
  name: d.get("name"),
  email: d.get("email"),
}));
Step 3

Fit It to Your Project

•
Indexes: a query that mixes equality and range filters, or orders by another field, usually needs a composite index. The first run fails with a link that creates it.
•
Document IDs: the converter treats a column called id as the document ID. If your documents keep id as an ordinary field, change documentId() to "id".
•
Paging: the web SDK has no OFFSET, so an OFFSET is done by reading extra documents and skipping them in code. For real paging, keep the last document and pass it to startAfter().
Step 4

Copy or Download

Copy the code into your React or Node.js project, or download it as a .js file. The same SQL can also become a MongoDB query or DynamoDB request params.

Frequently Asked Questions — SQL to Firestore

Which Firebase SDK does the code use?

It is written for the Firebase JavaScript SDK with the modular API, the one you import from "firebase/firestore" (version 9 and later). OR conditions use the or() and and() filters, which arrived in version 9.18, and COUNT(*) uses getCountFromServer(). The code expects a db variable from getFirestore(app) and uses await, so put it inside an async function. The Node.js Admin SDK has a chained API (db.collection("users").where(...)) and isn't what this page writes.

How does the WHERE clause turn into where() filters?

SQL's order of operations is kept, so a = 1 OR b = 2 AND c = 3 becomes or(a == 1, and(b == 2, c == 3)), and parentheses are respected. Firestore has no NOT, so a negation is pushed into the conditions: NOT (a = 1) becomes !=, NOT IN becomes not-in, and NOT BETWEEN becomes two ranges joined with or(). IN becomes an in filter, BETWEEN a pair of >= and <= filters, and LIKE 'abc%' a range from "abc" up to the next possible prefix. Firestore takes at most 30 values in an in filter, 10 in not-in and 30 OR branches in total; when a query goes over, a NOTE line says Firestore will reject it and how to split it.

What happens to conditions Firestore cannot express?

In a SELECT, a condition such as LOWER(name) = 'ann', LIKE '%.com' or a comparison between two columns is left out, and a NOTE line names it. The query then returns extra documents that you filter in your code, but a left-out condition never makes it miss one. For COUNT, UPDATE and DELETE that would give a wrong count or touch documents the SQL leaves alone, so the converter stops and explains which condition is the problem. JOINs, subqueries, GROUP BY and CASE have no Firestore form and also stop with an error.

How are long numbers, decimals and dates written?

The web SDK sends numbers as JavaScript numbers, which hold whole numbers exactly only up to 9007199254740991. A bigger one, such as the ICCID 8901240544102066246, is written as the text "8901240544102066246" with a note, so no digit is lost; it then compares as text, so a range like > on it is left out rather than giving wrong results. Decimals are copied as typed (14.50 stays 14.50 in the code; Firestore stores the value 14.5). A quoted '00501' stays a string, while an unquoted 00501 is the number 501 in SQL and is written as 501 with a note. A date like '2024-01-01' is compared as text; if the field holds Timestamps, the note shows the Timestamp.fromDate() form to use instead.

How do INSERT, UPDATE and DELETE work, and what is id?

A column named id is taken as the document ID. INSERT with an id becomes setDoc(doc(db, "users", "u1"), …), and without one addDoc(), which picks a random ID. Several rows are written with writeBatch, up to 500 writes per batch. UPDATE … WHERE id = 'u1' becomes updateDoc() on that document, and DELETE … WHERE id = 'u1' becomes deleteDoc(). With any other WHERE clause, the matching documents are read with a query and then updated or deleted in batches of 500. SET visits = visits + 1 becomes increment(1). Note that setDoc replaces an existing document, while SQL INSERT would fail on a duplicate key.

How are NULL and missing fields handled?

IS NOT NULL becomes where(field, "!=", null), which matches documents where the field exists and isn't null, the same rows SQL returns. IS NULL becomes where(field, "==", null), which only finds documents where the field is stored as null: Firestore can't look for a field that is missing, and the output says so. orderBy() on a field also skips documents that don't have it. Firestore compares text exactly, upper and lower case included, while MySQL's LIKE usually ignores case.

Is my SQL uploaded anywhere?

No. The conversion runs in your browser; the query isn't sent to a server and nothing is kept. A reload starts with an empty editor.

Related converters: SQL to MongoDB · SQL to DynamoDB · SQL to Elasticsearch · MongoDB Query Formatter