MongoDB Shell

使用MongoDB Shell

MongoDB的标准发新版本中提供了mongo shell.它提供了一种具有对JavaScript语言和标准函数的完全访问权限的JavaScript环境.它为MongoDB数据库提供了一个完整接口

基本查询

导入数据

首先创建一个CSV文件来保存具有以下结构的学生记录:
name,gender,age,country
创建的1.csv文件内容如下:

name,gender,age,country
"ab","m",18,"china"
"cd","w",19,"japan"
"ab","w",20,"india"
"ab","m",21,"korean"
"ef","w",18,"cuba"

然后执行命令:
mongoimport -d zuckjet -c csv --type csv --headerline --file ~/csv/1.csv
上述参数含义:
-d:数据库名称
-c:集合名称
--type:导入文件类型
--heeaderline:表示第一行是列名,不需要导入
--file:表示文件所在目录

执行完上述命令后,就在名为zuckjet的数据库里面的csv集合中插入了5条文档数据.

创建和插入

输入db命令即可查看当前所处的数据库:

> db
zuckjet
> 

输入show dbs命令将查看所有数据库:

> show dbs
admin    0.000GB
frame    0.000GB
local    0.000GB
zuckjet  0.000GB
> 

输入show collections将显示当前数据库中的集合列表.

> show collections
csv
users
> 

输入db.csv.find()将显示csv集合中的文档:

> db.csv.find()
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "gender" : "m", "age" : 18, "country" : "china" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "gender" : "m", "age" : 21, "country" : "korean" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

可以看到所创建的5个文档都显示出来了.除了你添加到文档的字段以外,还会为所有文档生成一个附加的_id字段.
多有文档都必须具有一个唯一的_id字段.如果没有显示指定,那么MongoDB同样会自动分配一个作为唯一对象ID,就像上面所显示的那样.

显示创建集合

除了隐式创建集合,用户也可以显示创建一个集合,比如要创建一个名为test的集合:
db.createCollection("test")

使用循环插入文档

也可以使用一个for循环来将文档添加到集合.

> for(var i=1;i<=23;i++)db.test.insert({"name":"test user"+i,"age":10+i,"gender":"f","country":"india"})
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("596ea99db110c6c1ca92d4ab"), "name" : "test user1", "age" : 11, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4ac"), "name" : "test user2", "age" : 12, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4ad"), "name" : "test user3", "age" : 13, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4ae"), "name" : "test user4", "age" : 14, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4af"), "name" : "test user5", "age" : 15, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b0"), "name" : "test user6", "age" : 16, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b1"), "name" : "test user7", "age" : 17, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b2"), "name" : "test user8", "age" : 18, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b3"), "name" : "test user9", "age" : 19, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b4"), "name" : "test user10", "age" : 20, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b5"), "name" : "test user11", "age" : 21, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b6"), "name" : "test user12", "age" : 22, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b7"), "name" : "test user13", "age" : 23, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b8"), "name" : "test user14", "age" : 24, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4b9"), "name" : "test user15", "age" : 25, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4ba"), "name" : "test user16", "age" : 26, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4bb"), "name" : "test user17", "age" : 27, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4bc"), "name" : "test user18", "age" : 28, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4bd"), "name" : "test user19", "age" : 29, "gender" : "f", "country" : "india" }
{ "_id" : ObjectId("596ea99db110c6c1ca92d4be"), "name" : "test user20", "age" : 30, "gender" : "f", "country" : "india" }
Type "it" for more
> 

上述输出结果可知,已经通过for语句成功插入多条文档.
find命令会返回指向结果集的一个游标.相较于一次性在界面显示所有文档,游标会显示前20个文档并且等待请求遍历(it)以便显示其后的20个文档,以此类推,直到所有的结果集都被显示出来

显示指定_id进行插入

以下命令会显示指定_id字段:
db.test.insert({"_id":10,"name":"test"})

更新

update()方法默认会更新单个文档.如果需要更新所有符合选择条件的文档,那么可以通过设置multi选项为true来实现这一目的.
首先查看集合csv中的文档:

 db.csv.find()
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "gender" : "m", "age" : 18, "country" : "china" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "gender" : "m", "age" : 21, "country" : "korean" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

然后通过命令将所有gender为"m"的用户的country改为australia:

> db.csv.update({"gender":"m"},{$set:{"country":"australia"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.csv.find()
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "gender" : "m", "age" : 18, "country" : "australia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "gender" : "m", "age" : 21, "country" : "korean" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

检查输出,我们发现只更新了第一个文档记录,这是更新的默认行为.
现在我们修改update命令并且加入multi选项,使得多有gender为m的用户的country都设置为russia

> db.csv.update({"gender":"m"},{$set:{"country":"russia"}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.csv.find()
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "gender" : "m", "age" : 18, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "gender" : "m", "age" : 21, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

如结果所示,这次已经将所有符合条件的文档都更新了.

如果对已经存在于文档中的字段执行update()命令,那么将会更新该字段的值,如果该字段并不存在于文档中,那么该字段将会被添加到文档.以下命令将会将字段company添加到第一条文档:

db.csv.update({},{$set:{"company":"facebook"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.csv.find()
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "gender" : "m", "age" : 18, "country" : "russia", "company" : "facebook" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "gender" : "m", "age" : 21, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

使用update命令的$unset操作符从文档中移除字段,例如我们移除掉刚才添加的字段:

> db.csv.update({},{$unset:{"company":""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.csv.find()
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "gender" : "m", "age" : 18, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "gender" : "m", "age" : 21, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

删除

要删除一个集合中的文档,可以使用remove方法.如果指定一个条件,那么只有满足该条件的文档才会被删除.如果没有指定条件,则会删除所有的文档.
以下命令将会删除gender=m的文档:
db.csv.remove({"gender":"m"})
以下命令将会删除所有文档:
db.csv.remove({})
如果希望删除该集合,则需要使用drop命令:
db.csv.drop()

选择器

以下命令将会查询所有gender为w的用户:

> db.csv.find({"gender":"w"})
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

如果希望查询gender为w的且name为cd的用户,则可以执行下列命令:

> db.csv.find({"gender":"w",$or:[{"name":"cd"}]})
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
> 

如果希望查询gender为w的且name为cd或ab的用户,则可以执行下列命令:

> db.csv.find({"gender":"w",$or:[{"name":"cd"},{"name":"ab"}]})
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
> 

投影器

投影器用来设置需要显示的具体细节或者字段.例如只需显示文档中name和age两个字段

> db.csv.find({},{"name":1,"age":1})
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "age" : 18 }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "age" : 19 }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "age" : 20 }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "age" : 21 }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "age" : 18 }
> 

sort()

在MongoDB中,排列顺序是按如下来指定的:1用于升序排列,而-1用于降序排列.
例如查询集合csv中的文档,并按降序排列:

> db.csv.find().sort({"age":-1})
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "gender" : "m", "age" : 21, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "gender" : "m", "age" : 18, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

limit()

limit用于限制结果集中的记录.例如,你希望返回结果集中的两个用户:

> db.csv.find().limit(2)
{ "_id" : ObjectId("596ddecbe9f518aa89aa753e"), "name" : "ab", "gender" : "m", "age" : 18, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa753f"), "name" : "cd", "gender" : "w", "age" : 19, "country" : "japan" }
> 

skip()

如果希望跳过前两个记录,则需要执行以下命令:

> db.csv.find().skip(2)
{ "_id" : ObjectId("596ddecbe9f518aa89aa7540"), "name" : "ab", "gender" : "w", "age" : 20, "country" : "india" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7541"), "name" : "ab", "gender" : "m", "age" : 21, "country" : "russia" }
{ "_id" : ObjectId("596ddecbe9f518aa89aa7542"), "name" : "ef", "gender" : "w", "age" : 18, "country" : "cuba" }
> 

findOne()

findOne()命令类似于find()命令.findOne()方法可以使用与find()一样的参数,但不同于返回一个游标,它会返回单个文档

> db.csv.findOne()
{
    "_id" : ObjectId("596ddecbe9f518aa89aa753e"),
    "name" : "ab",
    "gender" : "m",
    "age" : 18,
    "country" : "russia"
}

使用游标

在使用了find()方法时,MongoDB会将查询结果作为一个游标对象来返回.为了显示该结果,mongo shell会遍历所返回的游标.

> var c = db.csv.find({"gender":"m"})
> while(c.hasNext()) printjson(c.next())
{
    "_id" : ObjectId("596ddecbe9f518aa89aa753e"),
    "name" : "ab",
    "gender" : "m",
    "age" : 18,
    "country" : "russia"
}
{
    "_id" : ObjectId("596ddecbe9f518aa89aa7541"),
    "name" : "ab",
    "gender" : "m",
    "age" : 21,
    "country" : "russia"
}
> 

被分配给游标对象的变量还可以作为数组来操作.

>  var c = db.csv.find({"gender":"m"})
> printjson(c[1])
{
    "_id" : ObjectId("596ddecbe9f518aa89aa7541"),
    "name" : "ab",
    "gender" : "m",
    "age" : 21,
    "country" : "russia"
}
> 

explain()

explain()函数可用于查看在执行一个查询时MongoDB数据库当前运行的步骤。该函数有一些可选参数,描述了不同的详细级别:allPlansExecution,executionStats以及queryPlanner.默认的详细级别模式是queryPlanner.

你可能感兴趣的:(mongodb)