Embedded MongoDB Shell嵌入式MongoDB外壳
On this page本页内容
Starting in version 1.22, MongoDB Compass contains an embedded shell, 从1.22版本开始,MongoDB Compass包含一个嵌入式shell mongosh
. mongosh
is a JavaScript environment for interacting with MongoDB deployments. mongosh
。mongosh
是一个用于与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()
操作:
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
阶段:
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 您可以在Compass中禁用嵌入式MongoDB shell,以避免在mongosh
.mongosh
上运行未经授权的命令。
To disable the embedded MongoDB shell:要禁用嵌入式MongoDB外壳:
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查看mongosh
methods.mongosh
方法的完整列表。