文档菜单
文档首页
/
MongoDB for VS Code
/

/query 命令

在本页

  • 示例

/query 命令帮助从连接的 MongoDB 集群生成自然语言的查询。它MongoDB for Github Copilot 扩展提供了相关集合的底层架构信息,以便 GitHub Copilot 生成响应。如果您在提示中未指定集合,聊天将提示您选择相关集合。

当 LLM 生成查询时,您可以在游乐场文件中打开查询或在您的集合中直接运行查询。

考虑

users集合在 Mflix Sample Database中。集合中的每个文档具有以下结构

{
_id: {
"$oid": "59b99db4cfa9a34dcd7885b6"
},
name: "Kayden Washington",
email: "KW@email.com",
password: "11222021"
}

连接到包含

users集合的部署后,您可以向GitHub Copilot聊天请求生成一个查询,以找到具有name值为Kayden Washingtonusers集合中的文档。

@MongoDB /query In the sample_mflix database, find a document in the
users collection with the name of Kayden Washington.

GitHub Copilot聊天使用 MongoDB for Github Copilot 扩展来根据您数据库模式的知识生成以下查询

use(`sample_mflix`);
db.getCollection('users').findOne({ name: 'Kayden Washington' });

一旦 MongoDB for Github Copilot 扩展生成查询后,您可以选择直接运行查询或在一个沙盒中打开查询。

Screenshot of copilot generating a query
点击放大

您也可以使用 MongoDB for Github Copilot 扩展用于构建聚合管道。考虑Mflix 示例数据库中的 users 集合。该集合中的每个文档具有以下结构

{
_id: {
"$oid": "59b99db4cfa9a34dcd7885b6"
},
name: "Kayden Washington",
email: "KW@email.com",
password: "11222021"
}

连接到包含 users 集合的部署后,您可以让 GitHub Copilot 聊天生成聚合管道。

@MongoDB /query Generate an aggregation pipeline on the users
collection that first sorts documents alphabetically by name and then
removes the password field from each document.

MongoDB for Github Copilot 扩展生成以下聚合管道

use('sample_mflix');
db.getCollection('users').aggregate([
{ $sort: { name: 1 } },
{ $project: { password: 0 } }
]);

一旦 MongoDB for Github Copilot 扩展生成查询,您可以选择直接运行管道或在沙盒中打开管道。

Screenshot of copilot generating an aggregation pipeline
点击放大

您还可以迭代构建您的聚合管道

@MongoDB /query Add a stage to my pipeline that adds a username field
to each document containing the user's email without the
email domain.

MongoDB for Github Copilot 扩展返回以下聚合管道

use('sample_mflix');
db.getCollection('users').aggregate([
{ $sort: { name: 1 } },
{ $project: { password: 0 } },
{ $addFields: { username: { $arrayElemAt: [{ $split: ["$email", "@"] }, 0] } } }
]);
Screenshot of copilot iteratively building on an aggregation pipeline
点击放大

返回

MongoDB for Github Copilot 扩展

在本页