mongo Shell 工具

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

    同时 shell 也是 MongoDB distributions 的一个组件. 只要你 , 就可以通过 mongoshell 连接到你运行中的 MongoDB 实例.

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

    重要

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

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

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

      1. ./bin/mongo

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

    当你不加任何参数运行 mongo, 那么 shell 将会试图连接运行在 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 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 themyCollectionduring theinsertOne()operation:

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

    Theis one of themethods available in the mongo shell.

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

    If theshell 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()

    Theshell 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:

    Thedb.collection.find()method returns ato 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. Theshell 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 JSON formatting and equivalent to printjson()
    • printjson() to print with formatting and equivalent to print(tojson(<obj>))

    For more information and examples on cursor handling in themongoshell, see. See alsoCursor Helpfor list of cursor help in theshell.

    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. >

    Theshell 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':

      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, see

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

    SEE ALSO