Docs Home → MongoDB for VS Code
Export a Query or Pipeline to Language将查询或管道导出为语言
On this page本页内容
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#
- Java
- Node.js
- Python
- Ruby
Prerequisites先决条件
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:要打开包含默认模板的新演练场,请执行以下操作:
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.
- Command + Shift + P on macOS.
The Command Palette provides quick access to commands and keyboard shortcuts.“命令面板”提供了对命令和键盘快捷键的快速访问。
Find and run the "Create MongoDB Playground" command.找到并运行“创建MongoDB演练场”命令。
Use the Command Palette search bar to search for commands. 使用“命令面板”搜索栏可以搜索命令。All commands related to MongoDB for VS Code are prefaced with MongoDB:.所有与MongoDB for VS Code相关的命令都以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.当您运行“MongoDB:创建MongoDB演练场”命令时,MongoDB for VS Code会打开一个默认的演练场模板,该模板预先配置了一些命令。
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.要了解有关MongoDB for VS Code设置的更多信息,请参阅MongoDB for VSCode设置。
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.在上下文菜单中,选择要导出到的语言。MongoDB for VS Code opens a new VS Code window containing the highlighted code in your chosen language.MongoDB for 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)))
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))));
Export options vary by the selected export language.导出选项因所选导出语言而异。
Export an Aggregation Pipeline导出聚合管道
To export an aggregation pipeline:要导出聚合管道,请执行以下操作:
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' ] } } } }
]
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.在上下文菜单中,选择要导出到的语言。MongoDB for VS Code opens a new VS Code window containing the highlighted code in your chosen language.MongoDB for 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"))))))
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")))))));
Export options vary by the selected export language.导出选项因所选导出语言而异。