Docs Home / mongosh

Run Commands运行命令

To run commands in mongosh, you must first connect to a MongoDB deployment.要在mongosh中运行命令,您必须首先连接到MongoDB部署

Format Input and Output格式化输入和输出

mongosh uses the Node.js BSON parser parser to parse BSON data. mongosh使用Node.js BSON解析器来解析BSON数据。You can use the parser's EJSON interface to transform your data when you work with mongosh.使用mongosh时,可以使用解析器的EJSON接口转换数据。

For examples that use EJSON, see: EJSON.有关使用EJSON的示例,请参阅:EJSON

Switch Databases切换数据库

To display the database you are using, type db:要显示您正在使用的数据库,请键入db

db

The operation should return test, which is the default database.该操作应返回默认数据库test

To switch databases, issue the use <db> helper, as in the following example:要切换数据库,请发出use <db>帮助程序,如下例所示:

use <database>

To access a different database from the current database without switching your current database context, see the db.getSiblingDB() method.要在不切换当前数据库上下文的情况下访问与当前数据库不同的数据库,请参阅db.getSiblingDB()方法。

To list the databases available to the user, use the helper show dbs.要列出用户可用的数据库,请使用助手show dbs

Create a New Database and Collection创建新数据库和集合

To create a new database, issue the use <db> command with the database that you would like to create. 要创建新数据库,请对要创建的数据库发出use <db>命令。For example, the following commands create both the database myNewDatabase and the collection myCollection using the insertOne() operation:例如,以下命令使用insertOne()操作创建数据库myNewDatabase和集合myCollection

use myNewDatabase
db.myCollection.insertOne( { x: 1 } );

If a collection does not exist, MongoDB creates the collection when you first store data for that collection.如果集合不存在,MongoDB会在您首次存储该集合的数据时创建该集合。

Terminate a Running Command终止正在运行的命令

To terminate a running command or query in mongosh, press Ctrl + C.要终止mongosh中正在运行的命令或查询,请按Ctrl+C

When you enter Ctrl + C, mongosh:当您输入Ctrl+C时,mongosh

  • interrupts the active command,中断活动命令,
  • tries to terminate the ongoing, server-side operation, and尝试终止正在进行的服务器端操作,以及
  • returns a command prompt.返回命令提示符。

If mongosh cannot cleanly terminate the running process, it issues a warning.如果mongosh无法完全终止正在运行的进程,它会发出警告。

Note

Pressing Ctrl + C in mongosh does not terminate asynchronous code. mongosh中按Ctrl+C不会终止异步代码。Asynchronous operations such as setTimeout or operations like fs.readFile continue to run.setTimeout等异步操作或fs.readFile等操作将继续运行。

There is no way to terminate asynchronous code in mongosh. This is the same behavior as in the Node.js REPL.mongosh中无法终止异步代码。这与Node.js REPL中的行为相同。

Pressing Ctrl + C once will not exit mongosh, press Ctrl + C twice to exit mongosh.按一次Ctrl+C不会退出mongosh,按两次Ctrl+C退出mongos

You can also terminate a script from within the script code by calling the exit(<code>) command. For more information, refer to Terminate a Script on Error.您还可以通过调用exit(<code>)命令从脚本代码中终止脚本。有关更多信息,请参阅在出错时终止脚本

Command Exceptions命令例外

For commands whose output includes { ok: 0 }, mongosh returns a consistent exception and omits the server raw output. The legacy mongo shell returns output that varies for each command.对于输出包含{ ok: 0 }的命令,mongosh返回一致的异常并省略服务器原始输出。传统的mongo shell返回的输出因每个命令而异。

Clear the mongosh Console清除mongosh控制台

The cls command clears the console. You can also clear the console with Ctrl + L and console.clear().cls命令清除控制台。您还可以使用Ctrl+Lconsole.clear()清除控制台。