Built on Prettier
MongoDB shell queries are JavaScript: db.orders.aggregate([...]) is a method call whose argument is an array of objects. PasteKit therefore formats them with Prettier (JavaScript parser), the same formatter many teams already use for their application code, with two adjustments for the shell:
- mongosh helpers are kept. Lines such as
use shop,show collections,show dbs,itandexitare not valid JavaScript. They are set aside while Prettier runs and put back afterwards, so a whole script copied from a terminal session formats in one go. - Quoted keys stay quoted. Dotted paths like
"address.city"and positional operators like"items.$[i].backorder"must be quoted, and keys you quoted for consistency are left as you wrote them.
Everything else follows Prettier’s rules: objects that fit within the width stay on one line, longer ones expand one property per line, chained calls such as .find().sort().limit() break before each dot when the chain is long, and statements end with a semicolon. Trailing commas are added to multi-line objects and arrays, which mongosh accepts.
Settings and keyboard shortcuts
There are no MongoDB-specific options. Indentation and line width come from the toolbar. A width of 80 suits pipelines well: short stages such as { $limit: 10 } stay compact while $group and $lookup stages expand.
Use Ctrl/Cmd+Enter to format, Ctrl/Cmd+Shift+C to copy the result into mongosh, Compass or your code, and Ctrl/Cmd+K to open the command palette. Ctrl/Cmd+Shift+M does not minify shell scripts; for compact JSON filters use the JSON minifier.
Things to watch for
Shell helper functions — ISODate(), ObjectId(), NumberDecimal(), UUID() — are ordinary function calls to the parser and format without complaint. Extended JSON such as {"$oid": "..."} is also valid, because it is just an object.
Queries copied from MongoDB logs or the profiler are usually JSON rather than shell syntax; they format here too, but the JSON formatter gives you a tree view for exploring large documents.
Errors come from the JavaScript parser and point at the exact line and column. The most frequent are a missing closing brace or bracket at the end of a long pipeline, and a doubled or missing comma between stages. Because a pipeline is one big array, a single missing } can be reported several lines below where it belongs; formatting the part above it often makes the mismatch obvious.
Formatting never changes query semantics, but it does make two classic mistakes visible: a $match that comes after an expensive $unwind or $lookup instead of first, and a $sort without an index-friendly field order.
Examples
find with projection, sort and limit
The filter expands one condition per line, the projection fits on one line, and the chain breaks before each call.
db.users.find({age:{$gte:18},"address.city":"Singapore",tags:{$all:["beta","admin"]}},{name:1,email:1,_id:0}).sort({createdAt:-1}).limit(20)db.users
.find(
{
age: { $gte: 18 },
"address.city": "Singapore",
tags: { $all: ["beta", "admin"] },
},
{ name: 1, email: 1, _id: 0 },
)
.sort({ createdAt: -1 })
.limit(20);
updateMany with arrayFilters
The positional filtered operator path stays quoted, and each of the three arguments gets a line.
db.orders.updateMany({status:"paid","items.sku":"KB-104"},{$set:{"items.$[i].backorder":true},$currentDate:{updatedAt:true}},{arrayFilters:[{"i.sku":"KB-104","i.qty":{$gt:5}}]})db.orders.updateMany(
{ status: "paid", "items.sku": "KB-104" },
{ $set: { "items.$[i].backorder": true }, $currentDate: { updatedAt: true } },
{ arrayFilters: [{ "i.sku": "KB-104", "i.qty": { $gt: 5 } }] },
);
mongosh script with shell helpers
use and show lines are preserved exactly; the JavaScript statements are formatted around them.
use shop
show collections
db.orders.createIndex({customerId:1,created:-1},{name:"by_customer_recent"})
db.orders.countDocuments({status:{$ne:"cancelled"}})use shop
show collections
db.orders.createIndex(
{ customerId: 1, created: -1 },
{ name: "by_customer_recent" },
);
db.orders.countDocuments({ status: { $ne: "cancelled" } });
Common errors and how to fix them
| Error | Cause | Fix |
|---|---|---|
Unexpected token, expected "," | An object or array is not closed before the call ends, or two properties are missing the comma between them. | Go to the reported column and add the missing comma or closing } / ]. |
Unexpected token | A doubled comma, a stray colon or a shell-only construct the parser does not recognise. | Remove the extra punctuation at the reported position. |
Unterminated string constant. | A quoted field name or value is missing its closing quote. | Close the string on the same line. |
Frequently asked questions
Can I paste a whole aggregation pipeline array?
Yes. A bare array of stages, a db.collection.aggregate call or a driver call in JavaScript all format.
Does it work with Compass or Atlas queries?
Yes. Compass filters and pipeline stages are JavaScript object syntax, the same as the shell.
Why were semicolons added?
Prettier ends statements with semicolons. mongosh runs the script the same way with or without them.
Can it format Python (PyMongo) queries?
Not directly, because Python dictionaries need quoted keys and use True/False/None. Use the Python formatter for PyMongo code.