Help › Write SQL
Run a MongoDB script in mongosh
Turn on script mode to run a whole tab as one mongosh script, with variables, loops and functions, outside Querlyn's safety checks.
Querlyn 0.1.0 is not released yet. This help describes it ahead of release.
Querlyn reads MongoDB statements and never runs them as JavaScript; see
Query MongoDB. When a task needs
a script, with variables, loops, functions or anything else mongosh
accepts, turn on script mode: the tab then runs as one script in
mongosh, MongoDB's shell, installed on your computer.
const cutoff = new Date(Date.now() - 30 * 24 * 3600 * 1000);
for (const order of db.orders.find({ status: "open", placed: { $lt: cutoff } })) {
db.orders.updateOne({ _id: order._id }, { $set: { status: "stale" } });
print(`marked ${order._id}`);
}
Warning: A script runs outside Querlyn's safety checks. Querlyn cannot see what it does: nothing in it is checked, a read-only connection refuses it, and Production asks every time.
What you need
- mongosh on this computer. Querlyn does not include it and never
installs it: get it from https://www.mongodb.com/try/download/shell.
Querlyn looks for it on
PATHand in the usual install folders (Homebrew's on macOS, the installer's on Windows). To use another copy, set its path in Settings, under Tools, which also shows the copy Querlyn found and its version. - A MongoDB connection that signs in with a password, an X.509 certificate, AWS IAM or no sign-in. A connection that signs in with OIDC cannot hand its sign-in to mongosh: run that script with mongosh in a terminal.
Turn script mode on
In a MongoDB query tab, choose Script in the bar above the editor (, also ). While it is on:
- A banner above the editor says so, with Turn Off.
- Run (⌘↩ on macOS or Ctrl+Enter on Windows and Linux) runs the whole tab as one script; the button reads Run Script.
- Run Selection (⇧⌘↩ on macOS or Ctrl+Shift+Enter on Windows and Linux) runs only the selected text, as one script.
Script mode stays on for the tab until you turn it off or close the tab. It is not remembered when Querlyn restarts.
A tab whose file is a .js file starts in script mode: open the file
(, or drag it onto the window), or save a tab under a
name ending in .js. The banner says so, as when you turn it on
yourself, and on a Production connection each run still asks first. If
you turn script mode off in such a tab it stays off, also when you save
the file or read it again; opening the file anew starts in script mode
again. On a connection whose database has no script shell, a .js file
is text like any other. See Open and save SQL files.
Write a script
dbis the tab's database, as the database button above the editor shows it.use("sales")ordb.getSiblingDB("sales")reaches another database.- Print what you want to see. In a script, mongosh shows only what it
is told to: write
printjson(db.orders.findOne())orprint(db.orders.countDocuments({})), not the expression alone. - Everything mongosh accepts works:
const, loops, functions,load()of a file on this computer, its helpers.
Read the output
What the script prints appears under Messages as it runs, line by line. Lines mongosh writes as errors are red. The newest 2,000 lines are kept; earlier ones are counted at the top.
When the script ends, the run says how: finished, or failed with mongosh's exit code and its last error line. Results are text only: a script's documents do not appear in the grid.
(⌘. on macOS or Ctrl+. on Windows and Linux) stops mongosh and everything it started. Closing the tab stops it too.
Before a script runs
Querlyn checks, in this order, and nothing starts until each passes:
- A read-only connection refuses the script. Turn read-only off, or turn script mode off and run the statements, which Querlyn checks. See Read-only connections.
- mongosh must be found: when it is not, the run says where to get it.
- The question. Production asks every time a script runs, whatever the script holds, unless you turned Production's questions off. Other connections ask only where their setting asks about statements Querlyn cannot classify, which by default none does. The question names the script by its first line and its length; choose Run to go on. See Confirm risky statements.
Through an SSH route (below), a connection that verifies TLS in full also asks before mongosh skips the host-name check.
How mongosh connects
mongosh gets the connection's settings and its password, or the AWS
credentials Querlyn signed in with, in a file only you can read, in a
private folder that is deleted when the run ends, fails or is cancelled.
They never appear on mongosh's command line or in its environment, and
the password is removed from what mongosh prints. mongosh's own log
replaces credentials in a connection string with <credentials>.
- TLS: Verify full keeps mongosh's checks, against the connection's CA certificate or this computer's trusted certificates. Require and Prefer encrypt without checking the certificate; mongosh cannot fall back to an unencrypted connection as Prefer does. See Encrypt a connection.
- Through SSH, Kubernetes or AWS Systems Manager: Querlyn opens a
port on this computer for the run only, reachable from this computer
alone and, where the system can tell, from mongosh alone, and closes it
when the run ends. mongosh talks to the connection's host only, as
This server only does, so a write fails if that host is not the
primary. A DNS seed list (
mongodb+srv) cannot be reached this way. With Verify full, mongosh checks the certificate's chain but cannot check its host name, and Querlyn asks first. See Connect through SSH.
What is recorded
- Query history records the script like any run, under the connection's history setting: on a connection that keeps metadata only (Production by default), its time and outcome without its text. See Find a query you ran.
- The statement console lists the script as one entry: its text, or "mongosh script (12 lines)" on a connection whose history keeps metadata only. What mongosh itself sends to the server is not listed. See Watch what Querlyn sends.
What it does not do
- Transactions: the script runs on mongosh's own connection, not the tab's session. A transaction open in the tab does not include it, and Commit and Roll Back do not affect what the script did.
- Run Again in the query history opens the script in a new tab and runs it as statements, which Querlyn refuses where they are JavaScript. Turn on script mode in that tab and run it again.
Help for Querlyn 0.1.0. All help topics