mongo Shell 工具

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

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

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

    重要

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

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

    1. 输入./bin/mongo 来启动 mongo:

      1. ./bin/mongo

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

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

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

    1. db

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

    To list the available databases, use the helpershowdbs. See alsomethod 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 theoperation:

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

    Thedb.myCollection.insertOne()is one of the.

    • 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 alternativesyntax. 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 theshell, see:

    Themethod returns acursorto the results; however, in theshell, 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:

    • to print without formatting
    • print(tojson(<obj>))to print with 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 theshell, seeIterate a Cursor in the mongo Shell. See alsofor list of cursor help 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 (']'). Theshell 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. >

    Themongoshell supports keyboard shortcuts. For example,

    • Use the up/down arrow keys to scroll through command history. Seedocumentation 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':

      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

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

    SEE ALSO