翻译或纠错本页面

mongo shell

介绍

The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to query and update data as well as perform administrative operations.

The mongo shell is a component of the MongoDB distributions. Once you have installed and have started MongoDB, connect the mongo shell to your running MongoDB instance.

Most examples in the MongoDB Manual use the mongo shell; however, many drivers provide similar interfaces to MongoDB.

开始 mongo Shell

重要

在尝试启动 mongo shell前请确保MongoDB实例正在运行。

开始使用 mongo shell连接到你运行在**localhost默认端口号**的MongoDB实例。

  1. 在终端命窗口提示符(或Windows命令提示符)中,进入到你的Mongodb安装目录 <mongodb  installation dir>

    cd <mongodb installation dir>
    
  2. 输入``./bin/mongo``运行 mongo

    ./bin/mongo
    

    如果你已经添加``<mongodb 安装目录>/bin``到环境变量``PATH``中,则你只需直接输入``mongo``。

选项

如果你不带任何参数运行 mongomongo shell将尝试连接运行在``localhost``上端口号为``27017``的MongoDB实例。要指定不同的主机或端口号,以及其他选项,请参看 examples of starting up mongomongo reference 提供的可用选项的详细内容。

.mongorc.js 文件

Shell开始运行时, mongo 将在用户 HOME 目录下检查名为 .mongorc.js 的JavaScript文件。如果找到, mongo 将在首次命令行显示之前解释执行 .mongorc.js 的内容。如果你使用shell执行一个JavaScript或表达式,无论是通过命令行使用 --eval 选项,或通过指定 a .js file to mongomongo 都将在JavaScript 处理完成 之后 读取 .mongorc.js``文件。你可以通过使用 :option:`--norc` 选项禁止加载.mongorc.js``。

使用``mongo`` Shell

输入``db``,显示你当前正在使用的数据库:

db

此操作将返回默认数据库``test``, 要切换数据库,使用``use <db>``,如下例所示:

use <database>

使用``show dbs``列出所有可用的数据库。要在不切换当前数据库上下文(即 db)情况下,从当前数据库访问不同数据库,请参见 db.getSiblingDB() 方法。

你可以切换到一个不存在的数据库。当你第一次向数据库存储数据,如通过创建一个集合,MongoDB将自动创建数据库。例如,当执行 insert() 操作期间,数据库``myNewDatabase`` 和 collection ``myCollection``都将被创建。

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

The db.myCollection.insert() is one of the methods available in the mongo shell

  • ``db``指当前数据库。

  • ``myCollection``是集合的名称。

如果 mongo shell不接受集合的名称,例如如果名称中包含空格,连字符”-“,或者以数字开始,你可以使用代替语法来指代集合,如下所示:

db["3test"].find()

db.getCollection("3test").find()

更多 mongo shell中的MongoDB基本操作请参看如下文档:

格式化打印结果

The db.collection.find() method returns a cursor to the results; however, in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents that match the query. The mongo shell will prompt Type it to iterate another 20 times.

你可以在操作中添加``.pretty()``,以格式化打印结果,如下例所示:

db.myCollection.find().pretty()

此外,你可以在 mongo shell中使用下面的显式打印方法:

  • print() 无格式打印

  • print(tojson(<obj>))JSON 格式打印,等效于 printjson()

  • printjson()JSON 格式打印,等效于 print(tojson(<obj>))

For more information and examples on cursor handling in the mongo shell, see 在 mongo 命令行里迭代游标. See also 游标帮助 for list of cursor help in the mongo shell.

mongo Shell中的多行操作

如果你的代码行以左括号 ('('),左大括号 ('{')或左中括号 ('[') 结束,那么随后的一行将以省略号 ("...") 开始,直到你输入对应的右括号 (')'),右大括号 ('}')或右中括号 (']') 。 mongo shell在执行代码以前将一直等待右括号,右大括号或右中括号,如下例所示:

> if ( x > 0 ) {
... count++;
... print (x);
... }

你可以通过输入两行空白行以结束连续行模式,如下所示:

> if (x > 0
...
...
>

Tab命令补全和其他键盘快捷键

The mongo shell supports keyboard shortcuts. For example,

  • 使用上/下箭头键在历史命令中进行切换。更多信息参见 .dbshell 文档的``.dbshell``文件节。

  • 使用 <Tab> 键自动补全或列出可能的补全命令,在下面的示例中,即是使用 <Tab> 键补全以字母 'c' 开始的方法名:

    db.myCollection.c<Tab>
    

    由于Shell中有很多以字母``’c’开始的集合方法,所以使用``<Tab> 补全后将列出各种以``’c’``开头的方法。

完整快捷键列表,请参看 Shell Keyboard Shortcuts