Browse documentation
MongoG GuideVerified with mongodb 7.5.0

Work with the MongoG Query Editor

Use typed completions, database context, selected-statement execution, result panels, query modes, cancellation, saved scripts, and history.

A workspace for real driver code

The MongoG editor runs JavaScript or TypeScript against the selected connection. It supplies real objects from the installed official mongodb package instead of translating a small list of shell methods into custom requests.

That means collection methods, cursor options, sessions, transactions, BSON types, and future compatible driver APIs use their normal driver shapes. Monaco completions and types are generated from the driver version bundled with MongoG.

Start with connection context

Every query tab belongs to a connection and has an active database. The editor uses that context for execution and schema-aware field suggestions.

const learners = db.collection("learners");
learners.find({ active: true }).sort({ name: 1 });

Use use("database_name") when a script intentionally changes its active database, or call client.db("database_name") when you need two databases in the same script.

Run all or run a selection

Run the whole editor when the statements form one workflow. Select a statement or block when you want to execute only that part. MongoG uses its TypeScript syntax tree to identify statements; it does not split code on semicolons, so strings, comments, blocks, and multiline expressions remain correct.

For predictable maintenance work:

  1. Run the filter as a find() or countDocuments() first.
  2. Review the selected documents.
  3. Select and run the write statement.
  4. Run the verification query.
const filter = { tutorial: "editor", status: "stale" };
db.collection("jobs").find(filter);

db.collection("jobs").updateMany(filter, {
  $set: { status: "archived", archivedAt: ISODate() },
});

db.collection("jobs").find({ tutorial: "editor" });

Read statement results

MongoG keeps a result for each executable top-level statement. Values are classified as documents, a write result, a scalar, a change stream, or an opaque preview. Console output is associated with the active statement.

Returning a find() or aggregate() cursor opens an initial page. The cursor remains in the query runtime for Next and Previous controls. Large BSON values are represented as data and can be expanded without injecting executable markup.

print("Active records:", db.collection("jobs").countDocuments({ status: "active" }));
db.collection("jobs").find({ status: "active" });

Choose an execution mode

Query Mode is the default for database work. It does not provide Node process globals, network APIs, timers, imports, or require(). The provided driver context is enough for normal MongoDB scripts.

Trusted Script Mode is for code you trust and explicitly approve. It permits require("mongodb") and require("bson") only. It is equivalent to running trusted local code and is not an operating-system security sandbox.

Use Query Mode unless a script specifically needs the allowlisted module form.

Cancel and recover

Cancel requests are checked around automatically awaited operations, calls, and loop boundaries. MongoG waits for underlying driver work where possible and has a supervisor hard-stop fallback for blocked or CPU-bound scripts.

Cancellation cannot guarantee that a database server stopped an operation at the exact same instant. Design important writes to be idempotent, use appropriate maxTimeMS options, and verify the final database state after interruption.

Saved scripts and history

Saved scripts preserve reusable work. History records the source you executed and its context, making it possible to return to an investigation. Do not store secrets in either: connection details belong in profiles, and production exports belong in controlled files.

Use schema-aware completions as guidance

MongoG combines types generated from the bundled driver with sampled collection schema information. Driver completions help with method names and options; field completions help with paths observed for the active connection, database, and collection.

Schema suggestions are evidence from sampled data, not a server contract. A field can be absent from the sample and still exist, or appear with multiple BSON types across the collection. Use collection validation and application types for guarantees.

The schema cache is scoped by namespace and expires. Successful document mutations invalidate relevant cached information so later completions can reflect the new shape.

Structure maintenance scripts for review

Separate preparation, preview, mutation, and verification into visible blocks:

use("mongog_tutorial");
const jobs = db.collection("jobs");
const filter = { tutorial: "editor", status: "stale" };

// 1. Preview
jobs.find(filter, { projection: { title: 1, status: 1 } });

// 2. Mutate — select this statement only after reviewing the preview
jobs.updateMany(filter, {
  $set: { status: "archived", archivedAt: ISODate() },
});

// 3. Verify
print("Remaining stale jobs:", jobs.countDocuments(filter));

Comments do not enforce safety, but they make the intended selection workflow reviewable. For higher-risk scripts, add explicit candidate-count limits and throw when the observed state is outside expectations.

Recover from an execution error

An error stops later top-level statements in that run. Earlier acknowledged writes remain committed unless they were inside a transaction. Read the statement-level error, correct the source or data assumption, and run a verification query before retrying.

Practice by writing a four-block script that seeds tagged records, previews them, updates them, and verifies them. Run the preview selection alone, then the update, then the verification. Finally run the whole idempotent workflow and compare how MongoG presents each statement result.

Common mistakes

  • Assuming an editor selection is plain-text semicolon splitting misunderstands block execution; MongoG resolves it structurally.
  • Leaving a broad write statement next to exploratory reads makes accidental full execution more dangerous.
  • Switching to Trusted Script Mode does not grant extra MongoDB permissions; server roles still decide database access.
  • Treating cancellation as a transaction rollback can leave already acknowledged writes committed.
  • Treating sampled schema completion as proof that no other fields or types exist can produce incomplete maintenance logic.
  • Retrying from the top after a partial write without a verification step can apply non-idempotent changes twice.

Continue learning

Learn how query scripts and automatic await make driver code concise, or switch to the collection workspace for visual document editing. The editor follows the official Node.js driver guide and 7.5 API reference.