DocumentDB for VS Code

User ManualBack to User Manual


Query Runtime: How It Works Behind the Scenes

The Query Playground and Interactive Shell share a common query runtime that is bundled directly into the extension. This page explains how the runtime works, what powers the autocompletion and schema awareness, and introduces the open-source packages that make it all possible.

Table of Contents

No External Tools Required

Before v0.8, the extension’s scratchpad feature required a locally installed shell executable on your machine. That external dependency has been completely removed. The query runtime is now bundled into the extension itself.

This means:

How Queries Are Executed

When you run a query in the Query Playground or Interactive Shell, the extension:

  1. Reuses the existing connection: The runtime uses the database connection you already established when you connected to the cluster in the tree view. No new connection is created, and no credentials need to be re-entered.
  2. Evaluates in a sandboxed context: Your code runs in an isolated JavaScript context with access to the db object, BSON constructors (ObjectId, ISODate, etc.), and standard JavaScript globals.
  3. Runs in a worker thread: Each execution happens in a separate worker thread, so a slow or infinite query cannot freeze VS Code. If a query exceeds the timeout, the worker is terminated and a fresh one is created for the next run.
  4. Returns structured results: Results are normalized into a consistent format with metadata (document count, execution time, cursor state), then displayed in the results panel or terminal.

The Query Playground uses fresh context mode by default: each execution starts with a clean slate. The Interactive Shell uses persistent context mode: variables, functions, and state carry over between commands within a session.

How Autocompletion Works

Autocompletion across all query surfaces (Collection View, Query Playground, Interactive Shell) is powered by two data sources:

Field Names from Your Data

The extension samples documents from your collection and analyzes their structure to learn what fields exist and what types they hold. This analysis happens incrementally: as you query and browse data, the schema knowledge grows. Field names appear in completion suggestions with type annotations (e.g., name (String), age (Int32), tags (Array)).

Operators from Official Documentation

All supported DocumentDB API operators, stages, accumulators, BSON constructors, and system variables (over 300 in total) are available as completion items. Each entry includes a description, a code snippet, and a link to the official documentation page. The operator data is automatically sourced from the DocumentDB compatibility reference and the DocumentDB operator documentation, ensuring it stays up to date with the latest supported features.

The @documentdb-js Packages

The query runtime and autocompletion features are built on three open-source packages, developed as part of this extension and published under the @documentdb-js scope. Each package is standalone and can be used independently in other tooling.

@documentdb-js/shell-runtime

A sandboxed JavaScript evaluation engine for DocumentDB. It handles executing user code against a database, intercepting shell commands (help, exit, cls), transforming results into a consistent format, and generating help text.

The runtime supports two modes:

The runtime takes an existing database connection and wraps it for evaluation. It never opens or closes connections itself, which is why it works seamlessly with any authentication method the extension supports, including Entra ID.

📦 npm · Source

@documentdb-js/schema-analyzer

An incremental JSON Schema analyzer for DocumentDB and MongoDB API documents. It processes documents one at a time (or in batches) and produces an extended JSON Schema with statistical metadata:

The analyzer uses version-based caching so derived data (like the field list) is only recomputed when new documents are added.

📦 npm · Source

@documentdb-js/operator-registry

A reference catalog of all operators supported by DocumentDB. It contains over 300 entries across query operators, update operators, expression operators, aggregation stages, accumulators, window operators, BSON type constructors, and system variables. Each entry includes:

The operator data is automatically generated from the official DocumentDB documentation using a built-in scraper. A CI test validates that the package always matches the upstream documentation, so when DocumentDB adds or removes operators, the package is updated accordingly.

📦 npm · Source

Schema Sharing Across Features

The extension maintains a shared schema cache that accumulates knowledge about your collections as you work. When you browse documents in the Collection View, the schema analyzer processes those documents. That same schema data is then available to the Query Playground and Interactive Shell for autocompletion.

All schema analysis happens locally. The extension analyzes documents that are already being fetched for display. No additional requests are made to your database for schema purposes, and no document data is sent to external services. The schema information stays entirely within your VS Code instance.

This means:

Security and Isolation


Related: Query Playground Interactive Shell