安装及部署
学习一个新的数据库,当然首先第一步就是要来安装数据库以及部署数据库服务。
安装
- 在mongoDB官网下载相关的安装包,就像安装qq一样的安装mongodb。
- 我选择在我电脑上以F:\software\mongodb\server此路径来作为mongodb的安装路径。然后在server同级目录下创建data目录,在其下创建两个目录db和log,分别用于存放数据库数据和mongo的日志。如图所示:
部署运行
- 使用命令行在mongo的安装目录下的bin目录中运行如下命令:mongod.exe –dbpath F:\software\mongodb\data\db,指定数据库位置,如果没有输出异常信息,则表示启动mongoDB的服务端成功。这时就可以使用mongoDB了。
将mongoDB作为windows的服务。使用管理员来运行命令行。依旧进入到mongo的安装目录下的bin目录中,执行如下命令:mongod.exe –logpath “F:\software\mongodb\data\log\mongo.log” –logappend –dbpath “F:\software\mongodb\data\db” –serviceName “myMongo” –install。
以上命令解释(请猛戳这里去看看mongod参数详解):- logpath:日志所在路径
- logappend:日志追加
- dbpath:数据库路径
- serviceName:注册到windows的服务的名称
- install:安装到windows服务
当注册为windows服务后,可以在windows服务列表查看到刚刚新添加的服务。由于是在个人电脑上作为学习使用,所以通常我会将这种服务的启动方式设置为手动启动。也就是说,在我需要使用mongoDB的时候,我可以通过命令行命令net start myMongo来启动mongo的服务。这个命令在执行完步骤2的时候也会在日志文件中有所提示。
至此,mongoDB的安装及部署就完成了。
问题:
- 在刚开始安装运行的时候,可能会出现如下图所示的情况:
这是由于关闭了mongo的服务端,又去创建新的连接所导致的。
所以要么一直开着mongod,要么将其注册为windows的服务。只要服务一直在,mongoDB就在。 -
MongoDB的相关概念
下面的表格对关系型数据库mysql和非关系型数据库MongoDB做了相关概念的对比。
mysql MongoDB 解释 database database 数据库 table collection mysql称之为表,mongoDB称之为集合 row document mysql称之为数据行,mongoDB称之为数据文档 colunm field mysql称之为数据列,mongoDB称之为字段 primary key primary key mysql需要设置主键,mongoDB自动维护主键_id 下面是mongoDB常用的命令。
命令 解释 show dbs 显示所有的数据库名称 show collections 显示当前数据库中的所有集合 show users 显示当前数据库中的所有用户 show logs 显示可以访问的所有日志的名称 show log [name] 输出指定的日志,默认name为global use [db_name] 使用给定的数据库,如果没有,则为创建 MongoDB的使用
数据库的新增与删除
新增:使用下面的命令来新增数据库。当执行了use命令后,调用show dbs并不能看到刚刚新增的数据库,那是因为数据库中没有数据,所以咱们通过命令
db.集合名.insert
向新增的数据库中新建一个集合并为其插入一条数据,之后再使用show dbs命令时就可以看到刚刚新建的数据库了。> use earltest switched to db earltest > db earltest > show dbs local 0.000GB > db.mongotest.insert({"name":"练习"}) WriteResult({ "nInserted" : 1 }) > show dbs earltest 0.000GB local 0.000GB
删除:使用
db.集合名.drop()
来删除当前数据库下的指定的集合。使用db.dropDatabase()
来删除当前使用的数据库。删除之后,调用show dbs
就显示数据库已经被删除了。> use local switched to db local > db.test.drop() true > db.dropDatabase() { "dropped" : "local", "ok" : 1 } > show dbs >
集合的新增与删除
新增
通过使用命令db.createCollect(集合名)
来创建一个新的集合,例如:> show dbs earltest 0.000GB > db.createCollection("student") { "ok" : 1 } > show collections student
以上语句相当于SQL的建表语句
create table student(...)
,只是没有在建表时定义表结构,这就是关系型数据库与非关系型数据库最主要的区别。没有表结构的约束,那么mongoDB使用起来就更加灵活。其实可以不用执行创建集合的命令,因为在插入文档时,如果数据库中没有相应的集合,那么mongo会自动创建这个集合,并完成插入操作。删除
通过使用命令db.集合名.drop()
来删除指定的集合,例如:> show collections student > db.mongotest.drop() true > show collections >
以上语句相当于SQL的删表语句
drop table student
。
文档的增删改查
mongoDB采用的数据结构是一种类似于JSON的BSON格式,即Binary JSON,二进制JSON格式。
常见的数据类型有以下这些:数据类型 解释 String 字符串 Integer 整数类型,有32位和64位两种,分别记为Int32与Int64 Boolean 布尔类型,true还是false Double 浮点类型 Arrays 数组,可以存放多个某一类型的数据 Object ID 用于存储文档的ID 新增
通过命令db.集合名.insert(document)
来插入一个新的文档,例如:> db.createCollection("student") { "ok" : 1 } > show collections student >db.student.insert({"id":"1","name":"Adam","age":22,"sex":"male","major":"Psychology" }) WriteResult({ "nInserted" : 1 }) > db.student.find() { "_id" : ObjectId("57c83abfa33a42f78ac58e64"), "id" : "1", "name" : "Adam", "age" : 22, "sex" : "male", "major" : "Psychology" }
以上语句相当于SQL的向指定的表中插入一条数据,例如
insert into student values("1","Adam","22","male","Psychology")
文章最后附有student集合的初始数据,可供需要练习的读者直接练习使用。删除
通过命令db.集合名.remove(document)
来删除相关文档,document为过滤条件,例如:> db.student.find() { "_id" : ObjectId("57c83abfa33a42f78ac58e64"), "id" : "1", "name" : "Adam", "age" : 22, "sex" : "male", "major" : "Psychology" } { "_id" : ObjectId("57c83cc2a33a42f78ac58e65"), "id" : "2", "name" : "Alax", "age" : 21, "sex" : "male", "major" : "Biology" } > db.student.remove({"id":"2"}) WriteResult({ "nRemoved" : 1 }) > db.student.find() { "_id" : ObjectId("57c83abfa33a42f78ac58e64"), "id" : "1", "name" : "Adam", "age" : 22, "sex" : "male", "major" : "Psychology" }
以上语句相当于SQL的从指定表中删除相关数据
delete from student where id="2"
修改
通过命令db.集合名.update(parameter)
来更新相关文档数据。例如:> db.student.find() { "_id" : ObjectId("57c977b3058cb6872afd9e9d"), "id" : "1", "name" : "Adam", "age" : 23, "sex" : "male", "major" : "Psychology" } { "_id" : ObjectId("57c977b3058cb6872afd9e9e"), "id" : "2", "name" : "Alex", "age" : 21, "sex" : "male", "major" : "Biology" } { "_id" : ObjectId("57c977b3058cb6872afd9e9f"), "id" : "3", "name" : "Andy", "age" : 19, "sex" : "male", "major" : "Chemistry" } { "_id" : ObjectId("57c977b3058cb6872afd9ea0"), "id" : "4", "name" : "Bill", "age" : 20, "sex" : "male", "major" : "Economics" } { "_id" : ObjectId("57c977b3058cb6872afd9ea1"), "id" : "5", "name" : "Daisy", "age" : 20, "sex" : "female", "major" : "Sociology" } { "_id" : ObjectId("57c977b3058cb6872afd9ea2"), "id" : "6", "name" : "Elizabeth", "age" : 20, "sex" : "female", "major" : "Histroy" } { "_id" : ObjectId("57c977b3058cb6872afd9ea3"), "id" : "7", "name" : "Emily", "age" : 23, "sex" : "female", "major" : "Histroy" } { "_id" : ObjectId("57c977b3058cb6872afd9ea4"), "id" : "8", "name" : "Helena", "age" : 24, "sex" : "female", "major" : "Biology" } { "_id" : ObjectId("57c977b3058cb6872afd9ea5"), "id" : "9", "name" : "Julia", "age" : 23, "sex" : "female", "major" : "Economics" } { "_id" : ObjectId("57c977b3058cb6872afd9ea6"), "id" : "10", "name" : "Simon", "age" : 21, "sex" : "male", "major" : "Chemistry" } > db.student.update({"age":21},{$set:{"major":"Math"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.student.find() { "_id" : ObjectId("57c977b3058cb6872afd9e9d"), "id" : "1", "name" : "Adam", "age" : 23, "sex" : "male", "major" : "Psychology" } { "_id" : ObjectId("57c977b3058cb6872afd9e9e"), "id" : "2", "name" : "Alex", "age" : 21, "sex" : "male", "major" : "Math" } { "_id" : ObjectId("57c977b3058cb6872afd9e9f"), "id" : "3", "name" : "Andy", "age" : 19, "sex" : "male", "major" : "Chemistry" } { "_id" : ObjectId("57c977b3058cb6872afd9ea0"), "id" : "4", "name" : "Bill", "age" : 20, "sex" : "male", "major" : "Economics" } { "_id" : ObjectId("57c977b3058cb6872afd9ea1"), "id" : "5", "name" : "Daisy", "age" : 20, "sex" : "female", "major" : "Sociology" } { "_id" : ObjectId("57c977b3058cb6872afd9ea2"), "id" : "6", "name" : "Elizabeth", "age" : 20, "sex" : "female", "major" : "Histroy" } { "_id" : ObjectId("57c977b3058cb6872afd9ea3"), "id" : "7", "name" : "Emily", "age" : 23, "sex" : "female", "major" : "Histroy" } { "_id" : ObjectId("57c977b3058cb6872afd9ea4"), "id" : "8", "name" : "Helena", "age" : 24, "sex" : "female", "major" : "Biology" } { "_id" : ObjectId("57c977b3058cb6872afd9ea5"), "id" : "9", "name" : "Julia", "age" : 23, "sex" : "female", "major" : "Economics" } { "_id" : ObjectId("57c977b3058cb6872afd9ea6"), "id" : "10", "name" : "Simon", "age" : 21, "sex" : "male", "major" : "Chemistry" }
以上语句相当于SQL的更新语句
update student set major='Math' where age='21'
,但是可以看到满足age=21的记录有两条,通过mongoDB的update方法更新后,只更新了一条数据。那是因为mongoDB默认更新只更新找到的第一个记录,所以如果需要满足我们的更新需求,那么需要为update方法再传递一个参数,multi,如下所示:> db.student.update({"age":21},{$set:{"major":"Chinese"}},{multi:true}) WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.student.find() { "_id" : ObjectId("57c977b3058cb6872afd9e9d"), "id" : "1", "name" : "Adam", "age" : 23, "sex" : "male", "major" : "Psychology" } { "_id" : ObjectId("57c977b3058cb6872afd9e9e"), "id" : "2", "name" : "Alex", "age" : 21, "sex" : "male", "major" : "Chinese" } { "_id" : ObjectId("57c977b3058cb6872afd9e9f"), "id" : "3", "name" : "Andy", "age" : 19, "sex" : "male", "major" : "Chemistry" } { "_id" : ObjectId("57c977b3058cb6872afd9ea0"), "id" : "4", "name" : "Bill", "age" : 20, "sex" : "male", "major" : "Economics" } { "_id" : ObjectId("57c977b3058cb6872afd9ea1"), "id" : "5", "name" : "Daisy", "age" : 20, "sex" : "female", "major" : "Sociology" } { "_id" : ObjectId("57c977b3058cb6872afd9ea2"), "id" : "6", "name" : "Elizabeth", "age" : 20, "sex" : "female", "major" : "Histroy" } { "_id" : ObjectId("57c977b3058cb6872afd9ea3"), "id" : "7", "name" : "Emily", "age" : 23, "sex" : "female", "major" : "Histroy" } { "_id" : ObjectId("57c977b3058cb6872afd9ea4"), "id" : "8", "name" : "Helena", "age" : 24, "sex" : "female", "major" : "Biology" } { "_id" : ObjectId("57c977b3058cb6872afd9ea5"), "id" : "9", "name" : "Julia", "age" : 23, "sex" : "female", "major" : "Economics" } { "_id" : ObjectId("57c977b3058cb6872afd9ea6"), "id" : "10", "name" : "Simon", "age" : 21, "sex" : "male", "major" : "Chinese" }
update({query},{update},{multi})方法常用参数列表如下:
参数名 解释 query 相当于SQL的where update 相当于SQL的set multi 默认只更新找到的第一条记录,设置为true,则更新所有满足条件的记录 查询
接下来就是使用最多的查询操作了,对于查询的操作,我们与其来和SQL对比,这样更容易理解。
还是利用上面创建的学生表来进行查询。- 查询所有的学生
mongoDB:db.student.find()
SQL:select * from student
- 查询所有男生
mongoDB:db.student.find({"sex":"male"})
SQL:select * from student where sex='male'
- 查询年龄小于23岁的所有男生的id,姓名及专业
mongoDB:db.student.find({"age":{$lt:23}},{id:"",name:"",major:""})
SQL:select id,name,major from student where age<23
- 按照年龄降序查询所有学生
mongoDB:db.student.find().sort({age:-1})
SQL:select * from student order by age desc
- 统计学生总数
mongoDB:db.student.count()
SQL:select count(*) from student
- 统计历史专业的学生人数
mongoDB:db.student.count({"major":"Histroy"})
SQL:select count(*) from student where major='Histroy'
- 统计各个专业的人数
mongoDB:db.student.aggregate([{$group:{_id:"$major","人数":{$sum:1}}}])
SQL:select count(*) from student group by major
- 统计每个专业的学生人数,并求他们的平均年龄
mongoDB:db.student.aggregate([{$group:{_id:"$major","学生人数":{$sum:1},"平均年龄":{$avg:"$age"}}}])
SQL:select major,count(major) as '学生人数',avg(age) as '平均年龄' from student group by major
- 查询第二条到第七条的记录
mongoDB:db.student.find().limit(6).skip(1)
SQL:select * from student limit 1,6;
- 查询中文专业的男生学生信息
mongoDB:db.student.find({$and:[{major:"Chinese"},{sex:"male"}]})
SQL:select * from student where major='Chinese' and sex='male'
查询中文专业或者男生的学生信息
mongoDB:db.student.find({$or:[{major:"Chinese"},{sex:"male"}]})
SQL:select * from student where major='Chinese' or sex='male'
查询男生与女生的平均年龄
mongoDB:db.student.aggregate([{$group:{_id:"$sex","平均年龄":{$avg:"$age"}}}])
SQL:select avg(age) as '平均年龄' from student group by age
- 查询学号是1,3,4,5,6的学生信息
mongoDB:db.student.find({"id":{$in:["1","3","4","5","6"]}})
SQL:select * from student where id in("1","3","4","5","6")
- 查询以A开头学生姓名的学生信息
mongoDB:db.student.find({"name":/^A/})
SQL:select * from student where name like 'A%'
- 查询所有的学生
总结
以上就是近期对于mongoDB数据库的一点学习小总结。当然这只是简单的查询,还没有涉及到复杂的查询,这主要是针对于刚刚接触NoSQL数据库,对一直以来使用关系型数据库的养成的查询习惯来说,一时间还不是很适应。随着大数据时代的到来,我们将会更多的使用到mongoDB这样的非关系型数据库,所以,在接下来的学习工作中,对于非关系型数据库还是要多多的去了解,学习,使用。
附以student集合初始数据:
db.student.insert( [{"id":"1","name":"Adam","age":"22","sex":"male","major":"Psychology"}, {"id":"2","name":"Alex","age":"21","sex":"male","major":"Biology"}, {"id":"3","name":"Andy","age":"19","sex":"male","major":"Chemistry"}, {"id":"4","name":"Bill","age":"20","sex":"male","major":"Economics"}, {"id":"5","name":"Daisy","age":"20","sex":"female","major":"Sociology"}, {"id":"6","name":"Elizabeth","age":"20","sex":"female","major":"Histroy"}, {"id":"7","name":"Emily","age":"23","sex":"female","major":"Histroy"}, {"id":"8","name":"Helena","age":"24","sex":"female","major":"Biology"}, {"id":"9","name":"Julia","age":"23","sex":"female","major":"Economics"}, {"id":"10","name":"Simon","age":"21","sex":"male","major":"Chemistry"} ])