Starting in version 1.22, MongoDB Compass contains an embedded shell, 从1.22版本开始,MongoDB Compass包含一个嵌入式shell,即mongosh. mongosh。mongosh is a JavaScript environment for interacting with MongoDB deployments. mongosh是一个用于与MongoDB部署交互的JavaScript环境。You can use 您可以使用mongosh to test queries and operations in your database.mongosh来测试数据库中的查询和操作。
Open the Embedded MongoDB Shell打开嵌入式MongoDB Shell
To open the embedded 要打开嵌入式mongosh,you can either:mongosh,您可以:
Click >_ on the right of your deployment name in the Connections Sidebar.在“连接”侧栏中单击部署名称右侧的>_。Click >_Open MongoDB shell in the top right of any tab connected to a MongoDB deployment.单击连接到MongoDB部署的任何选项卡右上角的>_Open MongoDB shell。
Use the Embedded MongoDB Shell使用嵌入式MongoDB Shell
By default, mongosh, connects to the test database. To use a different database, run the following command in mongosh:
use <database name>
To run an operation in the embedded MongoDB Shell, type the operation into the shell and press Enter.要在嵌入式MongoDB Shell中运行操作,请在Shell中键入操作并按Enter键。
The following example runs a 以下示例运行db.collection.find() operation:db.collection.find()操作:
Example示例
db.employees.find( { "last_name": "Smith" } )Multi-Line Operations in the Embedded MongoDB Shell嵌入式MongoDB Shell中的多行操作
To write an operation that spans multiple lines in the embedded 要在嵌入式mongosh, begin with the first line, then press Shift + Enter to move to the next line of code.mongosh中编写跨多行的操作,请从第一行开始,然后按Shift+Enter移动到下一行代码。
When you are finished writing your operation, press Enter to run it.编写完操作后,按Enter键运行它。
The following multi-line example runs the 以下多行示例在聚合管道中运行$match stage in an aggregation pipeline:$match阶段:
Example示例
db.employees.aggregate( [ // press Shift + Enter
{ $match: { "last_name": "Smith" } } // press Shift + Enter
] ) // Press EnterDisable the Embedded MongoDB Shell禁用嵌入式MongoDB Shell
You can disable the embedded MongoDB shell in Compass to avoid running unauthorized commands on 您可以在Compass中禁用嵌入式MongoDB shell,以避免在mongosh.mongosh上运行未经授权的命令。
To disable the embedded MongoDB shell:要禁用嵌入式MongoDB shell:
In the MongoDB Compass menu, click Settings.在MongoDB Compass菜单中,单击“设置”。
Compass opens a dialog box where you configure your MongoDB Compass settings.Compass打开一个对话框,您可以在其中配置MongoDB Compass设置。
Learn More了解更多
The following links direct to the mongosh documentation, which contains more a complete reference for 以下链接直接指向mongosh, including syntax and behaviors.mongosh文档,其中包含更多关于mongosh的完整参考,包括语法和行为。
Learn how to perform CRUD operations in学习如何在mongosh.mongosh中执行CRUD操作。Learn how to run aggregation pipelines in学习如何在mongosh.mongosh中运行聚合管道。See a complete list of查看mongoshmethods.mongosh方法的完整列表。