Mongodb从0到1系列二:数据库与集合操作、文档增删改查
2017-07-27 23:05
791 查看
Mongodb从0到1系列一:下载、安装、启动、停止
在mongodb server启动之后,打开一个mongo shell.
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
MongoDB Enterprise > use test1
switched to db test1
MongoDB Enterprise > db.createCollection("teacher")
{ "ok" : 1 }
MongoDB Enterprise > db.createCollection("student")
{ "ok" : 1 }
MongoDB Enterprise > show collections
student
teacher
使用'db.COLLECTION_NAME.drop()'删除集合,其中COLLECTION_NAME表示集合名:
MongoDB Enterprise > db.student.drop()
true
MongoDB Enterprise > show collections
teacher
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
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 })
可以看到,同一个集合中的文档的结构并不要求一致,只要是键值对即可
MongoDB Enterprise > db.teacher.find()
修改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()
修改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()
修改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()
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()
删除的格式如下:
db.collection.remove(
<query>,
{
justOne: <boolean>,
writeConcern: <document>
}
)
query :(可选)删除的文档的条件。
justOne : (可选)如果设为 true 或 1,则只删除一个文档。
writeConcern :(可选)抛出异常的级别。
删除1:删除所有name为miaoqingsong的文档:
MongoDB Enterprise > db.student.remove({name:'miaoqingsong'})
WriteResult({ "nRemoved" : 2 })
MongoDB Enterprise > db.student.find()
删除2:删除第一条course为Chinese的文档:
MongoDB Enterprise > db.student.remove({course:'Chinese'},{justOne:true})
WriteResult({ "nRemoved" : 1 })
MongoDB Enterprise > db.student.find()
删除3:删除所有的文档:
MongoDB Enterprise > db.student.remove({})
WriteResult({ "nRemoved" : 2 })
MongoDB Enterprise > db.student.find()
MongoDB Enterprise >
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, teacherMongoDB 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(
<query>,
{
justOne: <boolean>,
writeConcern: <document>
}
)
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操作--文档、集合、数据库、shell
- mongodb 学习笔记之二 mongodb入门(数据库、文档和集合)
- NoSQL数据库_MongoDB_MongoDB 文档与集合
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- MongoDB入门---数据库&&&集合的基本操作
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- MongoDB 数据库、集合创建删除与文档插入
- 2.非关系型数据库(Nosql)之mongodb:mongodb显示所有数据库,使用数据库,集合创建(显示和隐式创建),集合查询,初步数据的增删改查,分页
- 【Mongodb教程 第十八课 】MongoDB常用命令 数据库命令 集合操作命令
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- mongodb概念--文档、集合、数据库、shell
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- mongodb入门-2 文档、集合、数据库
- nodejs-第四天-使用MongoDB、shell增删改查操作、聚集集合操作
- 4、MongoDB集合里面文档的插入,查找,更新操作
- Mongodb数组操作$(update)、$占位符更新嵌套数组、嵌套文档集合
- mongodb3.6系列教程3--集合操作