MongoDB Query Generator
Generate common MongoDB operations - find, insertOne/Many, updateOne/Many, deleteOne/Many, and countDocuments - as copyable code.
Choose an operation and fill in the collection, filter, and document/update fields - this tool generates the matching MongoDB code.
More Database & SQL Tools
View all 20Nothing here connects to a real MongoDB server - this only generates the code text for you to run in your own application or MongoDB shell.
What Does This Tool Generate?
Common MongoDB driver/shell operations - find, insertOne/insertMany, updateOne/updateMany, deleteOne/deleteMany, and countDocuments - with the collection name and filter/document you specify.
Why Use This Tool?
Remembering the exact method name and argument order/shape for each MongoDB operation (a filter object here, an update object with $set there) is easy to mix up when you don't write MongoDB code daily.
How to Use It
- Choose an operation.
- Enter the collection name and the relevant filter/document/update fields as JSON.
- Copy the generated code.
Limitations
This generates the operation code only - it never connects to or executes anything against a real MongoDB instance.
Examples
// Find paid orders over 1000, newest first
db.orders.find({ status: "paid", total: { $gt: 1000 } }).sort({ createdAt: -1 })
// Mark one order as shipped
db.orders.updateOne({ _id: ObjectId("...") }, { $set: { status: "shipped" } })
// Count customers in Pune
db.customers.countDocuments({ city: "Pune" })
SQL to MongoDB Cheat Sheet
| SQL | MongoDB |
|---|---|
SELECT * FROM orders WHERE status = 'paid' | db.orders.find({ status: "paid" }) |
WHERE total > 1000 | { total: { $gt: 1000 } } |
WHERE city IN ('Pune', 'Noida') | { city: { $in: ["Pune", "Noida"] } } |
UPDATE ... SET status = 'shipped' | updateMany(filter, { $set: { status: "shipped" } }) |
DELETE FROM ... WHERE ... | deleteMany(filter) |
SELECT COUNT(*) | countDocuments(filter) |
Common Mistakes
- Forgetting
$set:updateOne(filter, { status: "shipped" })is rejected by current drivers, and in older shells it replaced the whole document. - An empty filter in
deleteMany({})deletes every document - double-check before running. - Matching IDs as strings:
_idis usually anObjectId, not a plain string.
Working with relational databases too? See the SQL Query Generator. Format the JSON you pass in with the JSON Formatter.
Frequently Asked Questions
Explore More Tools
Keep going with related categories and our most used tools.