User Manual — Back to User Manual
The Query Playground is a file-based scripting environment for DocumentDB and MongoDB API databases, built directly into VS Code. It lets you write JavaScript queries, run them with a single click, and see formatted results in a side panel.
Each playground file uses the .documentdb.js extension and behaves like a regular JavaScript file with extra powers: connection awareness, inline execution, and autocompletion for your database schema.
Table of Contents
There are several ways to open a new Query Playground:
DocumentDB: New Query Playground.
The three inline icons next to each collection node are (from left to right): Open Collection View, New Query Playground, and Open Interactive Shell.
When a playground opens, a CodeLens header at the top displays the connection status, showing which cluster and database the file is connected to.
Playground files use standard JavaScript syntax. You have access to the db object, which represents the connected database:
// Find all active users older than 25
db.users.find({ status: 'active', age: { $gt: 25 } });
// Aggregate orders by status
db.orders.aggregate([{ $group: { _id: '$status', total: { $sum: '$amount' } } }, { $sort: { total: -1 } }]);
A playground file is divided into blocks separated by blank lines. Each block can be run independently:
// Block 1: Find recent orders
db.orders.find({ createdAt: { $gt: new Date('2025-01-01') } });
// Block 2: Count by category
db.products.aggregate([{ $group: { _id: '$category', count: { $sum: 1 } } }]);
You can also use variables, loops, and any JavaScript construct. Variables persist within a single execution (Run All), but not between separate block runs.
Because the playground uses a full JavaScript runtime, you can write queries the way you naturally would:
{ name: "Alice" } instead of { "name": "Alice" }{ status: 'active' }ObjectId("..."), ISODate("..."), NumberDecimal("...")Date.now(), Math.random(), new Date()The playground provides two execution modes, each triggered through CodeLens buttons that appear above your code:

| Mode | Trigger | Behavior |
|---|---|---|
| Run Block | Click the Run button above a specific block, or press Ctrl+Enter |
Executes the block at the cursor position |
| Run All | Click the Run All button at the top of the file, or press Ctrl+Shift+Enter |
Executes the entire file, top to bottom |
Each block also shows Collection View and Shell CodeLens links. These open the same query in the Collection View or the Interactive Shell, so you can switch tools without copy-pasting. Some scripts (e.g., loops, aggregations, or multi-statement blocks) may not be convertible to the Collection View.
When you press Ctrl+Enter and the cursor is on a blank line between blocks, the playground runs the nearest preceding block.
Each execution reuses the connection you established when you connected to the cluster. There is no need to authenticate again or provide credentials separately.
Each playground file runs in its own isolated worker thread. This means:
When you run code, results appear in a read-only tab next to your playground file. The results panel includes:
The results tab is stable: re-running code replaces the content in the same tab rather than opening new ones.
The playground supports console.log(), print(), and printjson() for debugging and inspection:
const users = db.users.find({ status: 'active' }).toArray();
console.log('Found', users.length, 'active users');
// print() and printjson() also work
print('Processing...');
printjson(users[0]);
Console output appears in a dedicated DocumentDB Query Playground Output channel in VS Code’s Output panel. When console output is produced, a hint is added to the results tab pointing you to the Output panel.
The playground provides rich autocompletion as you type:
Type db. to see a list of your database’s collections. After selecting a collection (e.g., db.users.), you will see all available collection methods: find(), findOne(), aggregate(), insertOne(), updateMany(), deleteOne(), and more.
Inside query objects, field names are suggested based on your collection’s actual data. The extension samples documents from the collection to learn its schema, then offers field names with their inferred BSON types (e.g., String, Int32, Double, Date, ObjectId, Array, Object) and an indicator if the field is sparse (not present in all documents):
db.users.find({
// Autocompletion suggests: name (String), age (Int32), email (String), createdAt (Date), ...
});
Hovering over a field name in your query shows a tooltip with the field’s inferred type. If a field holds multiple types across documents, all observed types are listed.
All DocumentDB API query operators ($gt, $lt, $in, $regex, etc.), aggregation stages ($match, $group, $sort, etc.), and BSON constructors (ObjectId(), ISODate(), etc.) are available with hover documentation.
When no schema data is available for a collection (e.g., when you open a playground for a collection you haven’t browsed yet), the autocompletion list shows a special “Discover fields in collection…“ entry. Selecting it samples approximately 100 documents from the collection to learn its field names and types. Once complete, field suggestions appear immediately.
You can also trigger this manually at any time through the Command Palette: run DocumentDB: Query Playground: Scan Collection Schema to refresh the schema data for the current collection.
Each playground file is permanently bound to the cluster and database it was created for. This means:
.documentdb.js files open at the same time, each connected to a different server or database.| Shortcut | Action |
|---|---|
Ctrl+Enter |
Run the current block |
Ctrl+Shift+Enter |
Run the entire file |
The playground is connected to the other query surfaces in the extension:
For more details on the Interactive Shell, see the Interactive Shell documentation.
The following settings control playground behavior:
| Setting | Default | Description |
|---|---|---|
documentDB.playground.confirmRunAll |
true |
Show a confirmation dialog before running the entire playground file with Run All |
documentDB.batchSize |
50 |
Number of documents to display per cursor iteration in the Query Playground and Interactive Shell |
console.log() for debugging: Print intermediate values to the Output panel while building complex aggregation pipelines.
Related: Interactive Shell How It Works Behind the Scenes