Docs Home / Compass

Embedded MongoDB Shell嵌入式MongoDB外壳

Starting in version 1.22, MongoDB Compass contains an embedded shell, mongosh. 从1.22版本开始,MongoDB Compass包含一个嵌入式shell,即mongoshmongosh 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 Enter

Disable the Embedded MongoDB Shell禁用嵌入式MongoDB Shell

You can disable the embedded MongoDB shell in Compass to avoid running unauthorized commands on mongosh.您可以在Compass中禁用嵌入式MongoDB shell,以避免在mongosh上运行未经授权的命令。

To disable the embedded MongoDB shell:要禁用嵌入式MongoDB shell:

1

In the Compass top menu bar, click MongoDB Compass.在Compass顶部菜单栏中,单击MongoDB Compass。

2

In the MongoDB Compass menu, click Settings.在MongoDB Compass菜单中,单击“设置”。

Compass opens a dialog box where you configure your MongoDB Compass settings.Compass打开一个对话框,您可以在其中配置MongoDB Compass设置。

3

Toggle Enable MongoDB Shell.切换“启用MongoDB Shell”。

If you select Set Read-Only Mode, Compass automatically unchecks the Enable MongoDB Shell setting.如果选择“设置只读模式”,Compass会自动取消选中“启用MongoDB Shell”设置。

4

Click Save.单击“保存”。

Learn More了解更多

The following links direct to the mongosh documentation, which contains more a complete reference for mongosh, including syntax and behaviors.以下链接直接指向mongosh文档,其中包含更多关于mongosh的完整参考,包括语法和行为。