Skip to main content
Database & SQL Tools

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.

Nothing 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

  1. Choose an operation.
  2. Enter the collection name and the relevant filter/document/update fields as JSON.
  3. 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

SQLMongoDB
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: _id is usually an ObjectId, 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

updateOne changes the first document that matches the filter; updateMany changes every matching document. The same applies to deleteOne and deleteMany.

Without an update operator such as $set, MongoDB would replace the whole document. $set changes only the fields you list.

In mongosh, MongoDB Compass's shell, or adapted for your driver (Node.js, Python and so on). The tool itself never connects to a database.
Share this tool: