对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

将查询或管道导出为语言

您可以使用适用于Github Copilot 的MongoDB扩展查询文档和聚合管道从 Playground 导出并转换为编程语言。您可以将查询和管道导出为以下语言:

  • C#

  • Go

  • Java

  • Node.js

  • PHP

  • Python

  • Ruby

  • Rust

必须启用MongoDB Extension for Github Copilot 才能使用 Export to Language功能。

您必须打开一个包含要导出的查询文档或管道的 Playground。

本页上的教程使用默认的 Playground 模板。

注意

您还可以使用适用于Github Copilot 的MongoDB扩展打开一个新的 Playground 来测试和导出查询。有关更多信息,请参阅使用 Copilot 导出和测试查询。

要打开包含默认模板的新 Playground,请执行以下操作:

1

在 Visual Studio Code 中,按下以下组合键之一:

  • Control + Shift + P (在 Windows 或 Linux 上)。

  • Command + Shift + P 在 macOS 上。

Command Palette 提供对命令和键盘快捷键的快速访问。

2

使用Command Palette搜索栏搜索命令。 与VS Code扩展相关的所有命令都以MongoDB:开头。

当您运行MongoDB: Create MongoDB Playground命令时, VS Code扩展会打开一个预先配置了一些命令的默认Playground 模板。

注意

要加载没有模板的新 Playground,请禁用 Use Default Template For Playground 设置。要了解有关 VS Code 扩展设置的更多信息,请参阅 Visual Studio 代码设置。

要导出查询文档,请执行以下操作:

1

突出显示 Playground 模板中的查询文档:

{ date: { $gte: new Date('2014-04-04'), $lt: new Date('2014-04-05') } }
2
  1. 单击 Playground 工具栏中的 Select Target for Export to Language 按钮。导出按钮位于 Play 按钮的左侧。

  2. 在上下文菜单中,选择要导出的目标语言。 VS Code扩展会打开一个新的VS Code窗口,其中包含所选语言的突出显示代码。

例如,将第 1 步中的查询文档导出到 Java 会生成以下代码:

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

您可以选择是否在导出的代码中包含导入声明和/或驱动程序语法。

在新打开的包含导出代码的 VS Code 窗口顶部,使用Import Statements和Driver Syntax开关来控制这些选项。

包含上述 Java 代码的 import 语句和驱动程序语法会产生以下输出:

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))));

注意

导出选项因所选导出语言而异。

要导出聚合管道,请执行以下操作:

1

突出显示 Playground 模板中的聚合管道:

[ { $match: { date: { $gte: new Date('2014-01-01'), $lt: new Date('2015-01-01') } } }, { $group: { _id: '$item', totalSaleAmount: { $sum: { $multiply: [ '$price', '$quantity' ] } } } }]
2
  1. 单击 Playground 工具栏中的 Select Target for Export to Language 按钮。导出按钮位于 Play 按钮的左侧。

  2. 在上下文菜单中,选择要导出的目标语言。 VS Code扩展会打开一个新的VS 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

您可以选择是否在导出的代码中包含导入声明和/或驱动程序语法。

在新打开的包含导出代码的 VS Code 窗口顶部,使用Import Statements和Driver Syntax开关来控制这些选项。

包含上述 Java 代码的 import 语句和驱动程序语法会产生以下输出:

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")))))));

注意

导出选项因所选导出语言而异。