MongoDB
Write a query
A MongoDB query tab runs JavaScript with the official Node.js driver. Three names are available in the code:
db: the database the query tab is opened in.db.ordersanddb.getCollection('orders')both return theorderscollection.ObjectId: builds an ObjectId, for exampleObjectId('65f1c0a2e4b0a1b2c3d4e5f6').params: the values from the Parameters panel, parsed as a JSON object or array.
When the whole query is one expression that starts with db, you can leave out return:
db.orders.find({ status: 'paid' }).sort({ createdAt: -1 })For longer code, end with return. You can use await:
const since = new Date(params.since);
return db.orders.aggregate([
{ $match: { createdAt: { $gte: since } } },
{ $group: { _id: '$status', total: { $sum: '$amount' } } },
]).toArray();A returned cursor or array becomes one row per document. A single document becomes one row. You do not need .toArray() on a cursor. The result is paged, and the full result is kept for export.
The code runs with the rights of the connection user, so write methods such as insertOne, updateMany and deleteMany change data. The tab's query timeout applies. See Query editor for running, parameters and saved queries.
Collections
In the explorer, each database has the groups Collections, Views, Indexes, GridFS buckets and Queries. The admin, local and config databases and system.* collections appear only when System objects is on at the top of the explorer.
- To create a collection, right-click Collections and choose Create collection.
- Right-click a collection for Open data, New query, Create index, Import, Copy data, Rename, Show metadata and Delete collection.
- In an open collection you can add, edit and delete documents. See Results and editing.
Show metadata shows the collection options, statistics, indexes, validator schema and the fields found in the 10 newest documents.
Views
Views appear under Views. You can open a view to read its documents, show its metadata and delete it with Delete view. Views are read-only in the grid. Create a view with a query, for example db.createCollection('paid_orders', { viewOn: 'orders', pipeline: [...] }).
Indexes
Indexes lists every index as collection.index.
- Right-click a collection or the Indexes group and choose Create index.
- Enter the Index name and pick the collection in Target if needed.
- Under Fields, add fields and choose a key type for each:
1,-1,textorhashed. - Under Options, turn on Unique, or turn on TTL and enter the lifetime in seconds.
- Check the command in Preview and click Create index.
To remove an index, right-click it and choose Delete index. The _id_ index cannot be deleted.
GridFS files
A bucket appears under GridFS buckets when a database has both <bucket>.files and <bucket>.chunks. These two collections are hidden from Collections. Open a bucket to list its files with name, size, upload date and content type.
- Upload: click Upload in the toolbar and choose one or more files. The content type comes from the file extension.
- Download: select files and click Download. One file is saved where you choose. Several files go to the folder you choose.
- Delete: select files and click Delete.
Explain a query
Click Explain in the query editor. QueryLane opens a new tab named Plan: … and runs the query with .explain('executionStats').
Explain works for a find or aggregate cursor. The query must be a single db... expression or end with return of a cursor or of .toArray(). QueryLane removes .toArray() for you. If your code already calls .explain(...), it runs as written.
return db.orders.find({ status: 'paid' }).toArray();A query that returns a variable, such as return documents;, cannot be explained. Rewrite the last line to return the cursor.