对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

嵌入式 MongoDB Shell

从版本1.22 开始, MongoDB Compass包含嵌入式Shellmongosh mongosh。 是用于与MongoDB部署交互的JavaScript环境。您可以使用mongosh 测试数据库中的查询和操作。

To open the embedded mongosh,you can either:

  • 单击 Connections Sidebar 中部署名称右侧的 >_

  • 单击与 MongoDB 部署相关的任何标签页右上角的 >_Open 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>

要在嵌入式 MongoDB Shell 中运行操作,请在 shell 中键入操作,然后按 Enter 键。

The following example runs a db.collection.find() operation:

例子

db.employees.find( { "last_name": "Smith" } )

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.

完成写入操作后,按 Enter 键运行。

The following multi-line example runs the $match stage in an aggregation pipeline:

例子

db.employees.aggregate( [ // press Shift + Enter
{ $match: { "last_name": "Smith" } } // press Shift + Enter
] ) // Press Enter

You can disable the embedded MongoDB shell in Compass to avoid running unauthorized commands on mongosh.

要禁用嵌入式 MongoDB Shell,请执行以下步骤:

1
2

Compass 打开一个对话框,您可以在其中配置 MongoDB Compass 设置。

3

如果您选择 Set Read-Only Mode,Compass 会自动取消选中 Enable MongoDB Shell 设置。

4

The following links direct to the mongosh documentation, which contains more a complete reference for mongosh, including syntax and behaviors.