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控制台
mongosh ConsoleThe cls command clears the console. You can also clear the console with Ctrl + L and console.clear().cls命令清除控制台。您还可以使用Ctrl+L和console.clear()清除控制台。