一、mongodb简介
http://www.ttlsa.com/mongodb/nosql-mongodb-description/
MongoDB组成部分:
- Database:可以有多个数据库,各个数据库间相互独立,每个数据库作为一个容器可以包含一个或多个集合(collection)。
- Collection:集合是一组文档(document)。在逻辑上相当于关系型数据库中的表。但是与表不同的是,在向集合存储数据时无需事先定义数据结构。
- Document:集合中存储文档的单位。一个文档包含了一系列字段或键值对。键是字符串,值可以是各种类型:字符串,整数,浮点数,时间戳,布尔值等等,甚至可以存储另一个文档,以json格式存储。
MongoDB数据交换格式:
- 文档的结构是一个JSON对象。当将这个文档存储于数据库中,将被序列化成一个特殊的二进制编码格式,成为BSON。
- BSON是MongoDB默认的数据交换格式。与XML和JSON相比BSON的优势在于内存的消耗和处理时间上更高效。JSON支持BSON支持的所有数据类型,同时还支持一些特殊的数据类型,比如正则表达式,对象ID,日期,二进制数据和代码。更好的移植性,编程语言如PHP,PYTHON,JAVA等等可以直接从BSON格式进行转换,易于与MongoDB融合和管理维护数据。
MongoDB与RDMS对比:
RDMS | database | table | row | column |
---|---|---|---|---|
MongoDB | database | collection | document | field |
MongoDB集合中没有外键,没有联合查询,约束性管理通常由应用层处理。因此其架构很灵活,也没有昂贵的alter table操作。
二、mongodb安装
官网下载最新版本
https://www.mongodb.com/download-center?jmp=nav#community
https://docs.mongodb.com/master/tutorial/install-mongodb-on-red-hat/
1、配置好yum源后进行安装
# vim /etc/yum.repos.d/mongodb-org-3.2.repo
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
# yum install -y mongodb-org
# rpm -qa |grep mongodb
mongodb-org-mongos-3.2.8-1.el7.x86_64
mongodb-org-server-3.2.8-1.el7.x86_64
mongodb-org-shell-3.2.8-1.el7.x86_64
mongodb-org-3.2.8-1.el7.x86_64
mongodb-org-tools-3.2.8-1.el7.x86_64
bsondump:
2、工具介绍
https://docs.mongodb.com/manual/reference/program/mongooplog/
https://docs.mongodb.com/manual/reference/program/mongoperf/
# rpm -ql mongodb-org-mongos |grep bin
/usr/bin/mongos ##分片路由,如果使用了sharding功能,则应用程序连接的是mongos而不是mongod
# rpm -ql mongodb-org-server |grep bin
/usr/bin/mongod ##数据库服务端,每个实例启动一个进程,可以fork为后台运行
# rpm -ql mongodb-org-shell |grep bin
/usr/bin/mongo ##客户端命令行工具,其实也是一个javascript解释器,支持javascript语法
# rpm -ql mongodb-org-tools |grep bin
/usr/bin/bsondump ##将bson格式的文件转储为json格式的数据
/usr/bin/mongodump ##数据库备份
/usr/bin/mongoexport ##数据导出
/usr/bin/mongofiles ##GridFS管理,可实现二制文件的存取
/usr/bin/mongoimport ##数据导入
/usr/bin/mongooplog ##从远程服务器的复制oplog中检查操作,并应用于本地服务器
/usr/bin/mongoperf ##测试磁盘I/O性能,可做mongo的I/O压力测试
/usr/bin/mongorestore ##数据恢复
/usr/bin/mongostat ##实时性能监控
/usr/bin/mongotop ##跟踪MongoDB的实例, 提供每个集合的统计数据。默认情况下,mongotop每一秒刷新一次
2、修改配置文件,创建数据库目录,启动服务
# cat /etc/mongod.conf |grep dbPath
dbPath: /mongodb/data
# mkdir -pv /mongodb/data ##默认不会自动创建数据库目录
mkdir: created directory ‘/mongodb’
mkdir: created directory ‘/mongodb/data’
# chown -R mongod.mongod /mongodb/data
# service mongod start
Starting mongod (via systemctl): [ OK ]
三、mongoDB的CRUD操作
http://www.ttlsa.com/mongodb/mongodb-insert-delete-select-update/
- select VS db.coll.find()
- insert operation
- update VS db.coll.update()
- delete VS db.coll.delete()
1、使用客户端命令连接到本机数据库,插入测试数据
# mongo
> help ##查看可以使用的命令
> db.help() ##获取数据库相关操作的方法
> for(i=1;i<100;i++) db.testcoll.insert({name:"user"+i,age:i,gender:"f",prefruits:["peach","strawberry"]})
WriteResult({ "nInserted" : 1 })
> show collections;
testcoll
2、删除年龄为3和姓名为user8的用户,并更新user7的年龄为77,查询testcoll中前7项文档
> db.testcoll.remove({age:3})
WriteResult({ "nRemoved" : 1 })
> db.testcoll.remove({name:"user8"})
WriteResult({ "nRemoved" : 1 })
> db.testcoll.update({name:"user7"},{$set:{age:77}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testcoll.find().limit(7)
{ "_id" : ObjectId("57b0038c871b1423b53cdbc2"), "name" : "user1", "age" : 1, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdbc3"), "name" : "user2", "age" : 2, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdbc5"), "name" : "user4", "age" : 4, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdbc6"), "name" : "user5", "age" : 5, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdbc7"), "name" : "user6", "age" : 6, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdbc8"), "name" : "user7", "age" : 77, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdbca"), "name" : "user9", "age" : 9, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
3、查询年龄>=15,<=17的文档
> db.testcoll.find({$and:[{age:{$gte:15}},{age:{$lte:17}}]})
{ "_id" : ObjectId("57b0038c871b1423b53cdbd0"), "name" : "user15", "age" : 15, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdbd1"), "name" : "user16", "age" : 16, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdbd2"), "name" : "user17", "age" : 17, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
4、查询包含某元素的文档
> db.testcoll.insert({name:"Celeste",age:22,gender:"m",fruit:["avocado","mangosteen"]})
WriteResult({ "nInserted" : 1 })
> db.testcoll.find({fruit:{$exists:true}})
{ "_id" : ObjectId("57b00a81871b1423b53cdc26"), "name" : "Celeste", "age" : 22, "gender" : "m", "fruit" : [ "avocado", "mangosteen" ] }
5、更新文档,mongoDB默认情况下只修改匹配到的第一个符合条件的文档指定multi为true
> db.testcoll.update({age:{$gt:95}},{$set:{gender:"m"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testcoll.find({age:{$gt:95}})
{ "_id" : ObjectId("57b0038c871b1423b53cdc21"), "name" : "user96", "age" : 96, "gender" : "m", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdc22"), "name" : "user97", "age" : 97, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdc23"), "name" : "user98", "age" : 98, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdc24"), "name" : "user99", "age" : 99, "gender" : "f", "prefruits" : [ "peach", "strawberry" ] }
> db.testcoll.update({age:{$gt:95}},{$set:{gender:"m"}},{multi:true})
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 3 })
> db.testcoll.find({age:{$gt:95}})
{ "_id" : ObjectId("57b0038c871b1423b53cdc21"), "name" : "user96", "age" : 96, "gender" : "m", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdc22"), "name" : "user97", "age" : 97, "gender" : "m", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdc23"), "name" : "user98", "age" : 98, "gender" : "m", "prefruits" : [ "peach", "strawberry" ] }
{ "_id" : ObjectId("57b0038c871b1423b53cdc24"), "name" : "user99", "age" : 99, "gender" : "m", "prefruits" : [ "peach", "strawberry" ] }
6、更新文档删除指定字段
> db.testcoll.update({name:"user98"},{$unset:{gender:""}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.testcoll.find({age:{$in:[98]}})
{ "_id" : ObjectId("57b0038c871b1423b53cdc23"), "name" : "user98", "age" : 98, "prefruits" : [ "peach", "strawberry" ] }
7、查询连接数
> db.serverStatus().connections
{ "current" : 1, "available" : 52427, "totalCreated" : NumberLong(6) }