mongo
Shell 工具
mongo
shell 是一个基于 JavaScript 语言的 MongoDB 交互工具. 你可以使用 shell 来查询和更新数据也可以执行管理员操作.
同时 mongo
shell 也是 的一个组件. 只要你 安装并且启动了 MongoDB, 就可以通过 shell 连接到你运行中的 MongoDB 实例.
在 MongoDB 手册 中的大部分例子都是使用 shell; 此外, 许多驱动 也提供了相似的工具.
重要
在试图启用 shell 之前确保 MongoDB 是运行的.
要启动 mongo
shell 并连接上运行在 localhost 和 默认端口 的 实例:
输入
./bin/mongo
来启动mongo
:./bin/mongo
如果你已经把
<存放mongodb工具的目录>/bin
添加到PATH
环境变量中, 就可以直接输入mongo
不用输入./bin/mongo
.
当你不加任何参数运行 , 那么 mongo
shell 将会试图连接运行在 localhost
且端口是 27017
的 MongoDB 实例. 要指定一个其他 host 或者 port, 或者其他选项, 参见和mongo 引用, 其中提供了详细的可用选项.
要查看当前使用的数据库库, 你可以输入 db
:
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 databasemyNewDatabase
and thecollectionmyCollection
during theoperation:
use myNewDatabase
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 themongo
shell 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:
db.getCollection("3 test").find()
db.getCollection("3-test").find()
db.getCollection("stats").find()
Themongo
shell 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 thevar
keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. Themongo
shell will promptTypeit
to 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 toprintjson()
printjson()
to print with JSON formatting and equivalent toprint(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 themongo
shell.
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:
> if ( x > 0 ) {
... count++;
... print (x);
... }
You can exit the line continuation mode if you enter two blank lines, as in the following example:
> if (x > 0
...
...
>
Themongo
shell supports keyboard shortcuts. For example,
Use the up/down arrow keys to scroll through command history. Seedocumentation for more information on the
.dbshell
file.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