OPTIONS
翻译或纠错本页面

Getting Started with the mongo Shell

本文提供一个基本的介绍使用 mongo 命令行.参见 Install MongoDB 为您的系统安装MongoDB

启动 mongo 命令行

启动 mongo 命令行连接到 本地主机 默认端口 运行您的MongoDB

  1. 到您的 <MongoDB安装目录>:

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

    ./bin/mongo
    

    If you have added the <mongodb installation dir>/bin to the PATH environment variable, you can just type mongo instead of ./bin/mongo.

  3. 显示您正在使用的数据库,输入 db :

    db
    

    该操作返回 test , 这是默认的数据库.要切换数据库,输入 use <db> .如下面的例子:

    use <database>
    

    要列出可用的数据库.使用 show dbs .参见 我可以临时连接另一个数据库吗? 从当前数据库访问不同的数据库,而无需切换当前的数据库环境(即db).

启动 mongo 命令行的其它选项,请参见 examples of starting up mongo 它提供可用选项的详细信息.

注解

在启动时, mongo 检查用户 HOME 目录名为 .mongorc.js 的JavaScript文件. 如果发现, mongo 显示的提示是首次解释 .mongorc.js 的内容.如果你使用命令行来评估一个JavaScript 文件或表达式,或者通过使用命令行选项 --eval 或指定的 a .js file tomongo , 之后 , mongo 将读取JavaScript处理完成的 .mongorc.js 文件.你可以使用选项 --norc 来防止 .mongorc.js 被加载.

执行查询

mongo 命令行,你可以使用 shell methods 来运行查询.如下面的例子:

db.<collection>.find()
  • db 是指当前的数据库.

  • <collection> 是查询的集合名称. 参见 Collection Help 列出可用的集合.

    如果该 mongo 命令行不接受的集合名称.例如:集合名称中包含 空格 , 连字符(-) ,或以 数字 开头,你可以使用替代语法来引用集合.如下面的例子:

    db["3test"].find()
    
    db.getCollection("3test").find()
    
  • find() 方法是JavaScript检索 <collection> 的方法. find() 返回一个游标的结果.然而,在 mongo 命令行,如果返回的游标没有分配给使用var关键字的变量.则游标被最多自动迭代20次,最多输出与查询匹配的头20个文档.该 mongo 命令行提示 Type it 迭代出其它20个文档.

    你可以设置 DBQuery.shellBatchSize 属性改变迭代默认值 20,在下面的例子中设置它为 10:

    DBQuery.shellBatchSize = 10;
    

    更多游标在:program:mongo 命令行的信息和处理的例子,参见 Cursors .

    参见 Cursor Help 游标在 mongo 命令行的帮助列表.

更多在 mongo 命令行的基本MongoDB操作,参见:

输出

mongo 命令行自动输出 find() 方法的结果.如果返回的游标没有分配给使用var关键字的变量.你可以添加 .pretty() 的操作,如下面的例子:

db.<collection>.find().pretty()

另外,你可以在 mongo 命令行下准确的输出该方法:

  • print() to print without formatting
  • print(tojson(<obj>)) to print with JSON formatting and equivalent to printjson()
  • printjson() to print with JSON formatting and equivalent to print(tojson(<obj>))

评估一个JavaScript文件

你可以在 mongo 命令行下执行一个 .js 文件.使用 load() 函数,如下面的例子:

load("myjstest.js")

这个函数加载和执行 myjstest.js 文件

The load() method accepts relative and absolute paths. If the current working directory of the mongo shell is /data/db, and the myjstest.js resides in the /data/db/scripts directory, then the following calls within the mongo shell would be equivalent:

load("scripts/myjstest.js")
load("/data/db/scripts/myjstest.js")

注解

There is no search path for the load() function. If the desired script is not in the current working directory or the full specified path, mongo will not be able to access the file.

使用自定义提示

你可以在命令行下创建修改提示内容的变量. prompt 变量可以保存字符串以及任意JavaScript.如果 prompt 保存函数返回一个字符串, mongo 可以显示每个动态的 prompt 信息.请看下面例子:

例子

创建与当前会话发出操作的数量提示,定义如下变量:

cmdCount = 1;
prompt = function() {
             return (cmdCount++) + "> ";
         }

该提示将类似于以下内容:

1> db.collection.find()
2> show collections
3>

例子

<database>@<hostname>$ 定义以下变量的形式创建一个 mongo 命令行提示符:

host = db.serverStatus().host;

prompt = function() {
             return db+"@"+host+"$ ";
         }

该提示将类似于以下内容:

<database>@<hostname>$ use records
switched to db records
records@<hostname>$

例子

要创建一个 mongo 命令行提示符包含系统时间和文件在当前数据库的数量,定义如下提示变量:

prompt = function() {
             return "Uptime:"+db.serverStatus().uptime+" Documents:"+db.stats().objects+" > ";
         }

该提示将类似于以下内容:

Uptime:5897 Documents:6 > db.people.save({name : "James"});
Uptime:5948 Documents:7 >

mongo 命令行使用外部编辑器

2.2 新版功能.

mongo 命令行可以使用 edit 操作在外部编辑器编辑一个功能或变量.编辑操作使用您的EDITOR环境变量的值.

在您的系统提示下你可以定义EDITOR变量,并开始 mongo 以下两个操作:

export EDITOR=vim
mongo

然后,考虑命令行会话下面的例子:

MongoDB shell version: 2.2.0
> function f() {}
> edit f
> f
function f() {
    print("this really works");
}
> f()
this really works
> o = {}
{ }
> edit o
> o
{ "soDoes" : "this" }
>

注解

作为外部编辑器在 mongo 命令行解释编辑代码时,它可以在功能上修改代码,这取决于JavaScript编译.对于 mongo 可以转换 1+1 to 2 或 remove comments.实际变化影响的代码只是外观,并会基于JavaScript使用的版本不同而异.但不会影响该代码的语义.

退出命令行

要退出命令行,键入 quit() 或者 使用 <Ctrl-c> 快捷方式.