mongo
Shell 工具
本页索引:
介绍
mongo
shell 是一个基于 JavaScript 语言的 MongoDB 交互工具. 你可以使用 mongo
shell 来查询和更新数据也可以执行管理员操作.
同时 mongo
shell 也是 MongoDB distributions 的一个组件. 只要你 安装并且启动了 MongoDB, 就可以通过 mongo
shell 连接到你运行中的 MongoDB 实例.
在 MongoDB 手册 中的大部分例子都是使用 mongo
shell; 此外, 许多驱动 也提供了相似的工具.
启动 mongo
Shell
重要
在试图启用 mongo
shell 之前确保 MongoDB 是运行的.
要启动 mongo
shell 并连接上运行在 localhost 和 默认端口 的 MongoDB 实例:
打开命令行工具, 找到你
<存放mongodb工具的目录>
:cd <存放mongodb工具的目录>
输入
./bin/mongo
来启动mongo
:./bin/mongo
如果你已经把
<存放mongodb工具的目录>/bin
添加到PATH
环境变量中, 就可以直接输入mongo
不用输入./bin/mongo
.
选项
当你不加任何参数运行 mongo
, 那么 mongo
shell 将会试图连接运行在 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
:
db
The operation should returntest
, which is the default database. To switch databases, issue theuse<db>
helper, as in the following example:
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 databasemyNewDatabase
and thecollectionmyCollection
during theinsertOne()
operation:
use myNewDatabase
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 themongo
shell 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:
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 themongo
shell, see:
- Getting Started Guide
- Insert Documents
- Query Documents
- Update Documents
- Delete Documents
- mongo Shell Methods
Format Printed Results
Thedb.collection.find()
method returns acursorto the results; however, in themongo
shell, 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:
db.myCollection.find().pretty()
In addition, you can use the following explicit print methods in themongo
shell:
print()
to print without formattingprint(tojson(<obj>))
to print with JSON formatting and equivalent toprintjson()
printjson()
to print with JSON formatting and equivalent toprint(tojson(<obj>))
For more information and examples on cursor handling in themongo
shell, seeIterate a Cursor in the mongo Shell. See alsoCursor Helpfor list of cursor help in themongo
shell.
Multi-line Operations 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 (']'
). Themongo
shell 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
...
...
>
Tab 补全及其他快捷键
Themongo
shell supports keyboard shortcuts. For example,
Use the up/down arrow keys to scroll through command history. See.dbshelldocumentation 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'
: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