mongo Shell 工具

本页索引:

介绍

mongo shell 是一个基于 JavaScript 语言的 MongoDB 交互工具. 你可以使用 mongoshell 来查询和更新数据也可以执行管理员操作.

同时 mongo shell 也是 MongoDB distributions 的一个组件. 只要你 安装并且启动了 MongoDB, 就可以通过 mongoshell 连接到你运行中的 MongoDB 实例.

MongoDB 手册 中的大部分例子都是使用 mongo shell; 此外, 许多驱动 也提供了相似的工具.

启动 mongo Shell

重要

在试图启用 mongoshell 之前确保 MongoDB 是运行的.

要启动 mongoshell 并连接上运行在 localhost默认端口MongoDB 实例:

  1. 打开命令行工具, 找到你 <存放mongodb工具的目录>:

    1. cd <存放mongodb工具的目录>
  2. 输入./bin/mongo 来启动 mongo:

    1. ./bin/mongo

    如果你已经把 <存放mongodb工具的目录>/bin 添加到 PATH 环境变量中, 就可以直接输入 mongo 不用输入 ./bin/mongo.

选项

当你不加任何参数运行 mongo, 那么 mongoshell 将会试图连接运行在 localhost 且端口是 27017 的 MongoDB 实例. 要指定一个其他 host 或者 port, 或者其他选项, 参见启动 mongo 实例mongo 引用, 其中提供了详细的可用选项.

.mongorc.js 文件

在启动时, mongo 检查用户的 HOME 目录下一个名为 .mongorc.js 的 JavaScript 文件. 如果文件存在, mongo 会在显示提示符之前第一时间加载 .mongorc.js 的内容. 当你使用 shell 来运行一个 JavaScript 文件或者表达式, 以及在命令行使用 --eval 选项或者指定一个 .js 文件让 mongo 运行时, mongo 会在 JavaScript 执行结束后读取 .mongorc.js 文件. 你也可以通过使用 --norc 选项阻止 .mongorc.js 被加载.

使用mongo Shell 工作

要查看当前使用的数据库库, 你可以输入 db:

  1. db

The operation should returntest, which is the default database. To switch databases, issue theuse<db>helper, as in the following example:

  1. use <database>

To list the available databases, use the helpershowdbs. See alsodb.getSiblingDB()method to access a different database from the current database without switching your current database context (i.e.db).

You can switch to non-existing databases. When you first store data in the database, such as by creating a collection, MongoDB creates the database. For example, the following creates both the databasemyNewDatabaseand thecollectionmyCollectionduring theinsertOne()operation:

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

Thedb.myCollection.insertOne()is one of themethods available in the mongo shell.

  • db
    refers to the current database.
  • myCollection
    is the name of the collection.

If themongoshell does not accept the name of a collection, you can use the alternativedb.getCollection()syntax. For instance, if a collection name contains a space or hyphen, starts with a number, or conflicts with a built-in function:

  1. db.getCollection("3 test").find()
  2. db.getCollection("3-test").find()
  3. db.getCollection("stats").find()

Themongoshell prompt has a limit of 4095 codepoints for each line. If you enter a line with more than 4095 codepoints, the shell will truncate it.

For more documentation of basic MongoDB operations in themongoshell, see:

Format Printed Results

Thedb.collection.find()method returns acursorto the results; however, in themongoshell, if the returned cursor is not assigned to a variable using thevarkeyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. Themongoshell will promptTypeitto iterate another 20 times.

To format the printed result, you can add the.pretty()to the operation, as in the following:

  1. db.myCollection.find().pretty()

In addition, you can use the following explicit print methods in themongoshell:

  • print()to print without formatting
  • print(tojson(<obj>))to print with JSON formatting and equivalent to printjson()
  • printjson() to print with JSON formatting and equivalent to print(tojson(<obj>))

For more information and examples on cursor handling in themongoshell, seeIterate a Cursor in the mongo Shell. See alsoCursor Helpfor list of cursor help in themongoshell.

Multi-line Operations in themongoShell

If you end a line with an open parenthesis ('('), an open brace ('{'), or an open bracket ('['), then the subsequent lines start with ellipsis ("...") until you enter the corresponding closing parenthesis (')'), the closing brace ('}') or the closing bracket (']'). Themongoshell waits for the closing parenthesis, closing brace, or the closing bracket before evaluating the code, as in the following example:

  1. > if ( x > 0 ) {
  2. ... count++;
  3. ... print (x);
  4. ... }

You can exit the line continuation mode if you enter two blank lines, as in the following example:

  1. > if (x > 0
  2. ...
  3. ...
  4. >

Tab 补全及其他快捷键

Themongoshell supports keyboard shortcuts. For example,

  • Use the up/down arrow keys to scroll through command history. See.dbshelldocumentation for more information on the.dbshellfile.

  • Use<Tab>to autocomplete or to list the completion possibilities, as in the following example which uses<Tab>to complete the method name starting with the letter'c':

    1. db.myCollection.c<Tab>

    Because there are many collection methods starting with the letter'c', the<Tab>will list the various methods that start with'c'.

For a full list of the shortcuts, seeShell Keyboard Shortcuts

Exit

To exit the shell, typequit()or use the<Ctrl-C>shortcut.

SEE ALSO