Mongodb从0到1系列二:数据库与集合操作、文档增删改查

Mongodb从0到1系列一:下载、安装、启动、停止


3. 数据库与集合操作

数据库与集合的创建、查看、删除操作
在mongodb server启动之后,打开一个mongo shell.

3.1 查看数据库

使用'show dbs'查看数据库:
db2a:~ # mongo
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
..
MongoDB Enterprise > show dbs
admin  0.000GB
local  0.000GB

3.2 创建/切换数据库

使用'use'创建/切换数据库:
MongoDB Enterprise > use test1
switched to db test1

3.3 创建集合

集合类似于关系数据库中的表,使用db.createCollection(name,options) 方法创建集合student, teacher
MongoDB Enterprise > db.createCollection("teacher")
{ "ok" : 1 }
MongoDB Enterprise > db.createCollection("student")
{ "ok" : 1 }

3.4 查看集合

使用'show collections'查看数据库中的集合:
MongoDB Enterprise > show collections
student
teacher


3.4 删除集合

使用'db.COLLECTION_NAME.drop()'删除集合,其中COLLECTION_NAME表示集合名:
MongoDB Enterprise > db.student.drop()
true
MongoDB Enterprise > show collections
teacher

3.5 删除数据库

使用'db.dropDatabase()'命令删除当前数据库
MongoDB Enterprise > show dbs
admin  0.000GB
local  0.000GB
test1  0.000GB
MongoDB Enterprise > db.dropDatabase()
{ "dropped" : "test1", "ok" : 1 }
MongoDB Enterprise > show dbs
admin  0.000GB
local  0.000GB

4. 文档增删改查

文档类似于关系数据库中的行, 集合其实是指文档的集合,集合创建完成之后,就可以在集合中增、删、改、查文档了:

4.1 增加/插入文档

往teacher里插入三条文档。如果集合不存在的情况下,直接插入文档,那么会自动创建该集合,命令不必在一行完成:

MongoDB Enterprise > use test1
switched to db test1
MongoDB Enterprise > db.createCollection("teacher")
{ "ok" : 1 }
MongoDB Enterprise > db.teacher.insert({name:'Wang',class:10,course:'math'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.teacher.insert({name:'Li',age:36,location:'Beijing',course:'English'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.teacher.insert({
... Firstname:'Miao',
... department:'GTS'
... })
WriteResult({ "nInserted" : 1 })

可以看到,同一个集合中的文档的结构并不要求一致,只要是键值对即可

4.2 查询文档

可以使用find()方法返回集合中的文档,加上pretty()的结果更加易读:
MongoDB Enterprise > db.teacher.find()
{ "_id" : ObjectId("596ef911e1bc0d5b8e646a65"), "name" : "Wang", "class" : 10, "course" : "math" }
{ "_id" : ObjectId("596ef915e1bc0d5b8e646a66"), "name" : "Li", "age" : 36, "location" : "Beijing", "course" : "English" }
{ "_id" : ObjectId("596ef91fe1bc0d5b8e646a67"), "Firstname" : "Miao", "department" : "GTS" }
MongoDB Enterprise > db.teacher.find().pretty()
{
        "_id" : ObjectId("596ef911e1bc0d5b8e646a65"),
        "name" : "Wang",
        "class" : 10,
        "course" : "math"
}
{
        "_id" : ObjectId("596ef915e1bc0d5b8e646a66"),
        "name" : "Li",
        "age" : 36,
        "location" : "Beijing",
        "course" : "English"
}
{
        "_id" : ObjectId("596ef91fe1bc0d5b8e646a67"),
        "Firstname" : "Miao",
        "department" : "GTS"
}

4.3 修改文档

有两个方法,一个是update(),主要用来更新;另一个是save(),主要用来替换已有文档

修改1:将teacher中,将name为"Li"的文档的course修改为math:
MongoDB Enterprise > db.teacher.update({'name':'Li'},{$set:{'course':'math'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.teacher.find().pretty()
{
        "_id" : ObjectId("596ef911e1bc0d5b8e646a65"),
        "name" : "Wang",
        "class" : 10,
        "course" : "math"
}
{
        "_id" : ObjectId("596ef915e1bc0d5b8e646a66"),
        "name" : "Li",
        "age" : 36,
        "location" : "Beijing",
        "course" : "math"
}
{
        "_id" : ObjectId("596ef91fe1bc0d5b8e646a67"),
        "Firstname" : "Miao",
        "department" : "GTS"
}

修改2:上面的命令只修改第一条找到的记录,如果要修改所有满足条件的记录,可以加上{multi:true},例如,要把所有course为math的文档的name修改为'Zhao'
MongoDB Enterprise > db.teacher.update({'course':'math'},{$set:{'name':'Zhao'}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
MongoDB Enterprise > db.teacher.find().pretty()
{
        "_id" : ObjectId("596ef911e1bc0d5b8e646a65"),
        "name" : "Zhao",
        "class" : 10,
        "course" : "math"
}
{
        "_id" : ObjectId("596ef915e1bc0d5b8e646a66"),
        "name" : "Zhao",
        "age" : 36,
        "location" : "Beijing",
        "course" : "math"
}
{
        "_id" : ObjectId("596ef91fe1bc0d5b8e646a67"),
        "Firstname" : "Miao",
        "department" : "GTS"
}

修改3:将id为"596ef915e1bc0d5b8e646a66"的文档替换为如下:
{
        "_id" : ObjectId("596ef915e1bc0d5b8e646a66"),
        "name" : "Li",
        "age" : 36,
        "location" : "Beijing",
        "course" : "English",
"class" : 15,
"department": "computer"
}

这时候可以使用save()方法,执行以下命令:
MongoDB Enterprise > db.teacher.save({
...         "_id" : ObjectId("596ef915e1bc0d5b8e646a66"),
...         "name" : "Li",
...         "age" : 36,
...         "location" : "Beijing",
...         "course" : "English",
... "class" : 15,
... "department": "computer"
... }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.teacher.find().pretty()
{
        "_id" : ObjectId("596ef911e1bc0d5b8e646a65"),
        "name" : "Zhao",
        "class" : 10,
        "course" : "math"
}
{
        "_id" : ObjectId("596ef915e1bc0d5b8e646a66"),
        "name" : "Li",
        "age" : 36,
        "location" : "Beijing",
        "course" : "English",
        "class" : 15,
        "department" : "computer"
}
{
        "_id" : ObjectId("596ef91fe1bc0d5b8e646a67"),
        "Firstname" : "Miao",
        "department" : "GTS"
}

4.4 删除文档

先在student集合中插入几条记录:
MongoDB Enterprise > db.student.insert({name:'miaoqingsong', course:'Computer'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.student.insert({name:'miaoqingsong', course:'English'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.student.insert({name:'Lilei', course:'Chinese'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.student.insert({name:'LiLei', course:'Chinese'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.student.insert({name:'LiLei', course:'Chinese'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.student.find()
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a68"), "name" : "miaoqingsong", "course" : "Computer" }
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a69"), "name" : "miaoqingsong", "course" : "English" }
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a6a"), "name" : "Lilei", "course" : "Chinese" }
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a6b"), "name" : "LiLei", "course" : "Chinese" }
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a6c"), "name" : "LiLei", "course" : "Chinese" }

删除的格式如下:

db.collection.remove(
   ,
   {
     justOne: ,
     writeConcern:
   }
)
query :(可选)删除的文档的条件。
justOne : (可选)如果设为 true 或 1,则只删除一个文档。
writeConcern :(可选)抛出异常的级别。

删除1:删除所有name为miaoqingsong的文档:
MongoDB Enterprise > db.student.remove({name:'miaoqingsong'})
WriteResult({ "nRemoved" : 2 })
MongoDB Enterprise > db.student.find()
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a6a"), "name" : "Lilei", "course" : "Chinese" }
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a6b"), "name" : "LiLei", "course" : "Chinese" }
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a6c"), "name" : "LiLei", "course" : "Chinese" }

删除2:删除第一条course为Chinese的文档:
MongoDB Enterprise > db.student.remove({course:'Chinese'},{justOne:true})
WriteResult({ "nRemoved" : 1 })
MongoDB Enterprise > db.student.find()
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a6b"), "name" : "LiLei", "course" : "Chinese" }
{ "_id" : ObjectId("596efa3ae1bc0d5b8e646a6c"), "name" : "LiLei", "course" : "Chinese" }

删除3:删除所有的文档:
MongoDB Enterprise > db.student.remove({})
WriteResult({ "nRemoved" : 2 })
MongoDB Enterprise > db.student.find()
MongoDB Enterprise > 

你可能感兴趣的:(mongodb)