Docs Home / VS Code Extension / Explore with Playgrounds

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:您可以使用Github Copilot的MongoDB扩展将查询文档和聚合管道从游乐场导出并翻译成编程语言。您可以将查询和管道导出为以下语言:

Prerequisites先决条件

You must enable the MongoDB Extension for Github Copilot to use the Export to Language feature.您必须启用Github Copilot的MongoDB扩展才能使用“导出到语言”功能。

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.您还可以打开一个新的游乐场,使用Github Copilot的MongoDB扩展测试和导出查询。有关更多信息,请参阅使用副副本导出和测试查询

To open a new playground containing the default template:要打开包含默认模板的新游乐场,请执行以下操作:

1

Open the Visual Studio Code Command Palette.打开Visual Studio Code“命令选项板”。

In Visual Studio Code, press one of the following key combinations:在Visual Studio Code中,按以下组合键之一:

  • Control + Shift + P on Windows or Linux.Windows或Linux上的Control+Shift+P
  • Command + Shift + P on macOS.macOS上的Command+Shift+P

The Command Palette provides quick access to commands and keyboard shortcuts.“命令选项板”提供对命令和键盘快捷键的快速访问。

2

Find and run the "Create MongoDB Playground" command.找到并运行“创建MongoDB游乐场”命令。

Use the Command Palette search bar to search for commands. All commands related to VS Code Extension are prefaced with MongoDB:.使用“命令选项板”搜索栏搜索命令。所有与VS Code扩展相关的命令都以MongoDB开头:。

When you run the MongoDB: Create MongoDB Playground command, VS Code Extension opens a default playground template pre-configured with a few commands.当您运行“MongoDB:创建MongoDB游乐场场”命令时,VS Code扩展会打开一个预先配置了几个命令的默认游乐场模板。

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.要了解有关VS Code扩展设置的更多信息,请参阅Visual Studio Code设置

Export a Query Document导出查询文档

To export a query document:要导出查询文档,请执行以下操作:

1

Highlight the code you want to export.突出显示要导出的代码。

Highlight the query document from the playground template:从操场模板中突出显示查询文档:

{ date: { $gte: new Date('2014-04-04'), $lt: new Date('2014-04-05') } }
2

Export your selection.导出选择。

  1. Click the Select Target for Export to Language button in the playground toolbar. The export button is to the left of the Play button.单击游乐场工具栏中的“选择导出到语言的目标”按钮。导出按钮位于“播放”按钮的左侧。
  2. 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.在上下文菜单中,选择要导出到的语言。VS Code扩展打开一个新的VS Code窗口,其中包含所选语言的突出显示的代码。

For example, exporting the query document from Step 1 to Java results in the following code:例如,将查询文档从步骤1导出到Java会得到以下代码:

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

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.在新打开的包含导出代码的VS Code窗口的顶部,使用“导入语句”和“驱动程序语法”开关来控制这些选项。

Including both import statements and driver syntax for the preceding Java code results in this output:在输出中同时包含前面Java代码的导入语句和驱动程序语法:

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:要导出聚合管道,请执行以下操作:

1

Highlight the code you want to export.突出显示要导出的代码。

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

Export your selection.导出选择。

  1. Click the Select Target for Export to Language button in the playground toolbar. The export button is to the left of the Play button.单击游乐场工具栏中的“选择导出到语言的目标”按钮。导出按钮位于“播放”按钮的左侧。
  2. 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.在上下文菜单中,选择要导出到的语言。VS Code扩展打开一个新的VS Code窗口,其中包含所选语言的突出显示的代码。

For example, exporting the pipeline from Step 1 to Java results in the following code:例如,将管道从步骤1导出到Java会得到以下代码:

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

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.在新打开的包含导出代码的VS Code窗口的顶部,使用“导入语句”和“驱动程序语法”开关来控制这些选项。

Including both import statements and driver syntax for the preceding Java code results in this output:在输出中同时包含前面Java代码的导入语句和驱动程序语法:

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.导出选项因所选导出语言而异。