Export a Query or Pipeline to Language
You can export and translate query documents and aggregation pipelines from a playground into a programming language using the MongoDB Extension for Github Copilot. You can export queries and pipelines to the following languages:
C#
Go
Java
Node.js
PHP
Python
Ruby
Rust
Note
Prerequisites
You must enable the MongoDB Extension for Github Copilot to use the Export to Language feature.
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.
Note
You can also open a new playground to test and export queries with the MongoDB Extension for Github Copilot. For more information, see Export and Test Queries with Copilot.
To open a new playground containing the default template:
Find and run the "Create MongoDB Playground" command.
Use the Command Palette search bar to search for commands. All commands related to VS Code Extension are prefaced with MongoDB:.
When you run the MongoDB: Create MongoDB Playground command, VS Code Extension 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 VS Code Extension settings, see Visual Studio Code Settings.
Export a Query Document
To export a query document:
Export your selection.
When you highlighted your code, a light bulb icon appeared. Click the icon.
In the context menu, choose the language you want to export to. VS Code Extension 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)))
Configure Export Options
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.
Export an Aggregation Pipeline
To export an aggregation pipeline:
Export your selection.
When you highlighted your code, a light bulb icon appeared. Click the icon.
In the context menu, choose the language you want to export to. VS Code Extension 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"))))))
Configure Export Options
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.