Embedded MongoDB Shell嵌入式MongoDB外壳

Starting in version 1.22, MongoDB Compass contains an embedded MongoDB Shell. 从1.22版开始,MongoDB Compass包含一个嵌入式MongoDB外壳The MongoDB Shell is a fully functional JavaScript environment for interacting with MongoDB deployments. MongoDB Shell是一个功能齐全的JavaScript环境,用于与MongoDB部署交互。You can use the MongoDB Shell to test queries and operations directly with your database.您可以使用MongoDB Shell直接使用数据库测试查询和操作。

To open the embedded MongoDB Shell, click MongoSH Beta along the bottom of MongoDB Compass.要打开嵌入式MongoDB外壳,请单击MongoDB Compass底部的“MongoSH Beta”。

When you initially open the MongoDB Shell, it connects to the test database. 最初打开MongoDB Shell时,它会连接到test数据库。To switch to a different database, run the following command in the MongoDB Shell:要切换到其他数据库,请在MongoDB Shell中运行以下命令:

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键。

Example
db.employees.find( { "last_name": "Smith" } )
Tip
See also:参阅:

To write an operation that spans multiple lines in the embedded MongoDB Shell, press Shift + Enter to begin the next line of code.要在嵌入式MongoDB Shell中编写跨多行的操作,请按Shift+Enter开始下一行代码。

When you are finished writing your operation, press Enter to run it.编写完操作后,按Enter键运行该操作。

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

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