Docs Menu

Docs HomeDevelop ApplicationsMongoDB for VS Code

Export a Query or Pipeline to Language

On this page

  • Prerequisites
  • Export a Query Document
  • Export an Aggregation Pipeline

You can export and translate query documents and aggregation pipelines from a playground into a programming language. You can export queries and pipelines to the following languages:

  • C#

  • Go

  • Java

  • Node.js

  • PHP

  • Python

  • Ruby

  • Rust

You must open a playground that contains a query document or pipeline you want to export.

The tutorials on this page use the default playground template.

To open a new playground containing the default template:

1

In Visual Studio Code, press one of the following key combinations:

  • Control + Shift + P on Windows or Linux.

  • Command + Shift + P on macOS.

The Command Palette provides quick access to commands and keyboard shortcuts.

2

Use the Command Palette search bar to search for commands. All commands related to MongoDB for VS Code are prefaced with MongoDB:.

When you run the MongoDB: Create MongoDB Playground command, MongoDB for VS Code opens a default playground template pre-configured with a few commands.

Note

To load new Playgrounds without the template, disable the Use Default Template For Playground setting. To learn more about MongoDB for VS Code settings, see MongoDB for VS Code Settings.

To export a query document:

1

Highlight the query document from the playground template:

{ date: { $gte: new Date('2014-04-04'), $lt: new Date('2014-04-05') } }
2
  1. When you highlighted your code, a light bulb icon appeared. Click the icon.

  2. In the context menu, choose the language you want to export to. MongoDB for VS Code opens a new VS Code window containing the highlighted code in your chosen language.

For example, exporting the query document from Step 1 to Java results in the following code:

new Document("date", new Document("$gte", new java.util.Date(1396569600000L))
.append("$lt", new java.util.Date(1396656000000L)))
3

You can choose whether to include import statements, driver syntax, or both in your exported code.

At the top of the newly opened VS Code window containing your exported code, use the Import Statements and Driver Syntax toggles to control these options.

Including both import statements and driver syntax for the preceding Java code results in this output:

import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.conversions.Bson;
import java.util.concurrent.TimeUnit;
import org.bson.Document;
/*
* Requires the MongoDB Java Driver.
* https://mongodb.github.io/mongo-java-driver
*/
MongoClient mongoClient = new MongoClient(
new MongoClientURI(
"mongodb://localhost:27017/?readPreference=primary&appname=mongodb-vscode+0.7.0&directConnection=true&ssl=false"
)
);
MongoDatabase database = mongoClient.getDatabase("mongodbVSCodePlaygroundDB");
MongoCollection<Document> collection = database.getCollection("sales");
FindIterable<Document> result = collection.aggregate(new Document("date", new Document("$gte", new java.util.Date(1396569600000L))
.append("$lt", new java.util.Date(1396656000000L))));

Note

Export options vary by the selected export language.

To export an aggregation pipeline:

1

Highlight the aggregation pipeline from the playground template:

[
{ $match: { date: { $gte: new Date('2014-01-01'), $lt: new Date('2015-01-01') } } },
{ $group: { _id: '$item', totalSaleAmount: { $sum: { $multiply: [ '$price', '$quantity' ] } } } }
]
2
  1. When you highlighted your code, a light bulb icon appeared. Click the icon.

  2. In the context menu, choose the language you want to export to. MongoDB for VS Code opens a new VS Code window containing the highlighted code in your chosen language.

For example, exporting the pipeline from Step 1 to Java results in the following code:

Arrays.asList(new Document("$match",
new Document("date",
new Document("$gte",
new java.util.Date(1388534400000L))
.append("$lt",
new java.util.Date(1420070400000L)))),
new Document("$group",
new Document("_id", "$item")
.append("totalSaleAmount",
new Document("$sum",
new Document("$multiply", Arrays.asList("$price", "$quantity"))))))
3

You can choose whether to include import statements, driver syntax, or both in your exported code.

At the top of the newly opened VS Code window containing your exported code, use the Import Statements and Driver Syntax toggles to control these options.

Including both import statements and driver syntax for the preceding Java code results in this output:

import java.util.Arrays;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.conversions.Bson;
import java.util.concurrent.TimeUnit;
import org.bson.Document;
/*
* Requires the MongoDB Java Driver.
* https://mongodb.github.io/mongo-java-driver
*/
MongoClient mongoClient = new MongoClient(
new MongoClientURI(
"mongodb://localhost:27017/?readPreference=primary&appname=mongodb-vscode+0.7.0&directConnection=true&ssl=false"
)
);
MongoDatabase database = mongoClient.getDatabase("mongodbVSCodePlaygroundDB");
MongoCollection<Document> collection = database.getCollection("sales");
FindIterable<Document> result = collection.aggregate(Arrays.asList(new Document("$match",
new Document("date",
new Document("$gte",
new java.util.Date(1388534400000L))
.append("$lt",
new java.util.Date(1420070400000L)))),
new Document("$group",
new Document("_id", "$item")
.append("totalSaleAmount",
new Document("$sum",
new Document("$multiply", Arrays.asList("$price", "$quantity")))))));

Note

Export options vary by the selected export language.