Docs HomeMongoDB Compass

Embedded MongoDB Shell嵌入式MongoDB外壳

Starting in version 1.22, MongoDB Compass contains an embedded shell, mongosh. mongosh is a JavaScript environment for interacting with MongoDB deployments. 从1.22版本开始,MongoDB Compass包含一个嵌入式shell mongoshmongosh是一个用于与MongoDB部署交互的JavaScript环境。You can use mongosh to test queries and operations in your database.您可以使用mongosh测试数据库中的查询和操作。

Open the Embedded MongoDB Shell打开嵌入式MongoDB外壳

To open the embedded mongosh, click _MONGOSH at the bottom of the MongoDB Compass window.要打开嵌入的mongosh,请单击MongoDB Compass窗口底部的_MONGOSH

Use the Embedded MongoDB Shell使用嵌入式MongoDB Shell

By default, mongosh, connects to the test database. 默认情况下,mongosh连接到test数据库。To use a different database, run the following command in mongosh:要使用其他数据库,请在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外壳

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外壳:

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参考,包括语法和行为。