Docs Home → MongoDB for VS Code
Explore Your Data with Playgrounds使用演练场探索您的数据
On this page本页内容
MongoDB Playgrounds are JavaScript environments where you can prototype queries, aggregations, and MongoDB commands with helpful syntax highlighting.MongoDB Playgrounds是JavaScript环境,在这里您可以通过有用的语法高亮显示来原型查询、聚合和MongoDB命令。
MongoDB Playgrounds provide intelligent autocomplete for:MongoDB Playgrounds为以下各项提供智能自动完成功能:
- The Mongo Shell API
MongoDB keywords, operators, aggregation stages, database, collection, and field names.MongoDB关键字、运算符、聚合阶段、数据库、集合和字段名称。Any JavaScript variables or keywords.任何JavaScript变量或关键字。
You can save playgrounds in your workspace and use them to document how your application interacts with MongoDB. 您可以将演练场保存在工作区中,并使用它们来记录应用程序如何与MongoDB交互。MongoDB for VS Code interprets files with the MongoDB for VS Code将带有.mongodb.js
extension as playgrounds..mongodb.js
扩展名的文件解释为演练场。
Starting in version 从0.11.0
the default file extension for MongoDB Playgrounds files is .mongodb.js
. 0.11.0
版本开始,MongoDB Playgrounds文件的默认文件扩展名是.mongodb.js
。Previous versions of MongoDB Playgrounds use the default file extension MongoDB Playgrounds的早期版本使用默认的文件扩展名.mongodb
. .mongodb
。MongoDB Playgrounds support both file extensions.MongoDB Playgrounds支持这两种文件扩展名。
MongoDB for VS Code splits your Playground window to display your Playground Result in the right-side pane. You can perform one of two actions on the result depending on the query you ran in the Playground.MongoDB for VS Code拆分您的“演练场”窗口,在右侧窗格中显示您的“演练场结果”。根据在演练场中运行的查询,可以对结果执行两个操作之一。
You may save any Playground Result to a file.您可以将任何“演练场结果”保存到文件中。You may edit any JSON document that MongoDB for VS Code returns in the Playground Result from a findOne or find query.您可以编辑MongoDB for VS Code在findOne或find查询的“演练场结果”中返回的任何JSON文档。MongoDB for VS Code returns these results in JSON format with JSON syntax highlighting. Each JSON document has an Edit Document link at its top.MongoDB for VS Code以“JSON”格式返回这些结果,并突出显示“JSON”语法。每个JSON文档的顶部都有一个“编辑文档”链接。Click Edit Document at the top of the document you want to edit.单击要编辑的文档顶部的“编辑文档”。MongoDB for VS Code opens this one JSON document in a new window as an editable document.MongoDB for VS Code在一个新窗口中将这一个“JSON”文档作为可编辑文档打开。Make any changes you need.做出你需要的任何改变。Press按Ctrl+S(macOS上的Cmd+S)将这些更改保存到数据库中。Ctrl+S
(Cmd+S
on macOS) to save those changes in the database.
Prerequisite先决条件
To run a playground, you must connect to a MongoDB deployment using MongoDB for VS Code. 要运行演练场,必须使用MongoDB for VS Code连接到MongoDB部署。To learn how to connect to a deployment, see either:要了解如何连接到部署,请参阅:
Open a Playground打开演练场
Create a New Playground创建新演练场
You can create a new playground from the Playgrounds panel, the MongoDB for VS Code Overview page, or the Microsoft Visual Studio Code您可以从“演练场”面板、MongoDB for VS Code “概览”页面或Microsoft Visual Studio Code Command Pallette.
Command Palette创建一个新的演练场。
Open a Saved Playground打开保存的演练场
If you have files in your Visual Studio Code workspace with the 如果您的Visual Studio Code工作区中有扩展名为.mongodb.js
or .mongodb
extension, MongoDB for VS Code displays these files in the Playgrounds panel..mongodb.js
或.mongodb
的文件,mongodb for VS Code会在“演练场”面板中显示这些文件。
Double click a file to open that playground.双击文件以打开该演练场。
Run a Playground运行演练场
To run a playground, click the Play Button in VS Code's top navigation bar. 要运行演练场,请单击VS Code顶部导航栏中的“播放”按钮。Your playground runs against the deployment specified in your active connection.您的演练场与活动连接中指定的部署背道而驰。
Connect to a Deployment from a Playground从演练场连接到展开
If MongoDB for VS Code is not connected to a MongoDB deployment, you can specify a connection string for the deployment you want to run your playground against.如果MongoDB for VS Code未连接到MongoDB部署,则可以为要运行您的演练场的部署指定连接字符串。
If you have a playground file open in Visual Studio Code and do not have an active connection, MongoDB for VS Code displays Click here to add connection at the top of your playground.如果您在Visual Studio Code中打开了一个演练场文件,但没有活动连接,则MongoDB for VS Code将在演练场顶部显示“单击此处添加连接”。

Click this link to open the connection string dialog.单击此链接打开连接字符串对话框。Enter the connection string for the deployment you want to run this playground against.输入要运行此演练场的部署的连接字符串。Press Enter.按Enter键。Run your playground.运行演练场。
When you connect to a deployment through the playground connection dialog, that connection is added to your MongoDB for VS Code Connections list.当您通过演练场连接对话框连接到部署时,该连接将添加到MongoDB for VS Code“连接”列表中。
Run Selected Lines of a Playground运行演练场的选定行
If you select a section of your playground, you may optionally run only the selected portion. 如果您选择了演练场的一部分,您可以选择只运行选定的部分。You may run either a single line or multiple lines of your playground.你可以在演练场上运行一行,也可以运行多行。
MongoDB for VS Code shows the Run Selected Lines from Playground link immediately above your selected section. MongoDB for VS Code在您选择的部分正上方显示“
运行演练场中的选中行”链接。Click this link to test and troubleshoot specific lines or sections of your playground.单击此链接可测试和排除演练场上特定线路或部分的故障。
Code Formatting and Linting Tools代码格式和Linting工具
MongoDB Playgrounds are JavaScript files and can work with popular Visual Studio Code formatting and linting extensions like PrettierMongoDB Playgrounds是JavaScript文件,可以与流行的Visual Studio Code格式和linting扩展(如Prettier and ESLint
.
和ESLint
)一起使用。
If you use a code formatting extension, MongoDB Playgrounds suggest autocompletion and liniting hints for:如果您使用代码格式化扩展,MongoDB Playgrounds建议您自动完成并为以下内容添加提示:
System variables such as系统变量,如$$ROOT
and$$NOW
$$ROOT
和$$NOW
use
anddb
commandsuse
和db
命令Collection names, in bracketed notation such as集合名称,用括号表示,例如db["collection"].find({})
Alternative commands for shell commands not supported in MongoDB Playgrounds such asMongoDB Playgrounds不支持shell命令的替代命令,例如show users
For a full list of autocompletion and linting improvements, see the v0.11.1 release notes有关自动完成和linting改进的完整列表,请参阅v0.11.1发行说明.
。
Log to Console from a Playground从演练场登录控制台
MongoDB for VS Code supports the following methods to log messages to the console. MongoDB for VS Code支持以下方法将消息记录到控制台。Logged messages appear in the Output panel in VSCode.记录的消息显示在VSCode的“输出”面板中。
console.log()
print()
printjson()
Logging to the console can be useful to track the output of certain commands in your playground, such as results after a particular query or aggregation.登录到控制台可以用来跟踪演练场上某些命令的输出,例如特定查询或聚合后的结果。
Tutorials教程
To learn how to use MongoDB Playgrounds for CRUD operations, see Perform CRUD Operations.要了解如何使用MongoDB Playgrounds进行CRUD操作,请参阅执行CRUD操作。To learn how to use MongoDB Playgrounds to run aggregation pipelines, see Run Aggregation Pipelines.要了解如何使用MongoDB Playgrounds运行聚合管道,请参阅运行聚合管道。
Consideration for Authentication身份验证的考虑因素
If your deployment requires authentication, your database user privileges may affect the actions you can perform using MongoDB for VS Code.如果您的部署需要身份验证,那么数据库用户权限可能会影响您可以使用MongoDB for VS Code执行的操作。