从版本1.22 开始, MongoDB Compass包含嵌入式Shellmongosh mongosh。 是用于与MongoDB部署交互的JavaScript环境。您可以使用mongosh 测试数据库中的查询和操作。
打开嵌入式 MongoDB Shell
To open the embedded mongosh,you can either:
单击 Connections Sidebar 中部署名称右侧的 >_。
单击与 MongoDB 部署相关的任何标签页右上角的 >_Open MongoDB shell(打开 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" } )
嵌入式 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.
完成写入操作后,按 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
禁用嵌入式 MongoDB Shell
You can disable the embedded MongoDB shell in Compass to avoid running unauthorized commands on mongosh.
要禁用嵌入式 MongoDB Shell,请执行以下步骤:
了解详情
The following links direct to the mongosh documentation, which contains more a complete reference for mongosh, including syntax and behaviors.
Learn how to perform CRUD operations in
mongosh.Learn how to run aggregation pipelines in
mongosh.