mongodb nosql 文档
一、概述1.1 MongoDB概念
MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。在高负载的情况下,添加更多的节点,可以保证服务器性能。MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。在nosql数据库里,大部分的查询都是键值对(key、value)的方式。MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中最像关系数据库的。其特征NoSQL、文档存储、Json数据模型、支持事务。
1.2 NoSQL概念
NoSQL,指的是非关系型的数据库。NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称。
NoSQL用于超大规模数据的存储。(例如谷歌或Facebook每天为他们的用户收集万亿比特的数据)。这些类型的数据存储不需要固定的模式,无需多余操作就可以横向扩展。
1.3 NoSQL的特征
高扩展性:Nosql去掉了关系数据库的关系型特性,易于横向扩展。
高性能:Nosql通过简单的key-value方式获取数据,非常快速。还有NoSQL的Cache是记录级的,是一种细粒度的Cache,所以NoSQL在这个层面上来说就要性能高很多。
灵活的数据模型:相对于关系数据库里,增删字段的繁琐,NoSQL无需事先为要存储的数据建立字段,随时可以存储自定义的数据格式。
高可用:NoSQL在不太影响性能的情况,就可以方便的实现高可用的架构。比如mongodb通过mongos、config server replica set,shard就可以快速配置出高可用配置。
1.4 NoSQL分类
类型 | 部分代表 | 特点 |
列存储 | Hbase Cassandra Hypertable | 顾名思义,是按列存储数据的。最大的特点是方便存储结构化和半结构化数据,方便做数据压缩,对针对某一列或者某几列的查询有非常大的IO优势。 |
文档存储 | MongoDB CouchDB | 文档存储一般用类似json的格式存储,存储的内容是文档型的。这样也就有有机会对某些字段建立索引,实现关系数据库的某些功能。 |
key-value存储 | Tokyo Cabinet / Tyrant Berkeley DB MemcacheDB Redis | 可以通过key快速查询到其value。一般来说,存储不管value的格式,照单全收。(Redis包含了其他功能) |
图存储 | Neo4J FlockDB | 图形关系的最佳存储。使用传统关系数据库来解决的话性能低下,而且设计使用不方便。 |
对象存储 | db4o Versant | 通过类似面向对象语言的语法操作数据库,通过对象的方式存取数据。 |
xml数据库 | Berkeley DB XML BaseX | 高效的存储XML数据,并支持XML的内部查询语法,比如XQuery,Xpath。 |
1.5 MongoDB概念解析
在mongodb中基本的概念是文档、集合、数据库,对应与关系型数据库如下表:
SQL术语/概念 | MongoDB术语/概念 | 解释/说明 |
---|---|---|
database | database | 数据库 |
table | collection | 数据库表/集合 |
row | document | 数据记录行/文档 |
column | field | 数据字段/域 |
index | index | 索引 |
table joins | 表连接,MongoDB不支持 | |
primary key | primary key | 主键,MongoDB自动将_id字段设置为主键 |
数据库:一个MongoDB 实例可以承载多个数据库。它们之间可以看作相互独立,每个数据库都有独立的权限控制。在磁盘上,不同的数据库存放在不同的文件中。MongoDB 中存在以下系统数据库。
集合:集合就是一组文档,类似于关系数据库中的表。集合是无模式的,集合中的文档可以是各式各样的。在MongoDB中集合用collections表示,每个
collection用一个名字标识,需要注意以下几点:
文档:文档是 MongoDB 中数据的基本单位,类似于关系数据库中的行(但是比行复杂)。多个键及其关联的值有序地放在一起就构成了文档。
二、安装部署
2.1 配置yum安装
配置mongodb的yum源进行安装
- cat >/etc/yum.repos.d/mongodb.repo<<EOF
- [mongodb]
- name=MongoDB Repository
- baseurl=
- gpgcheck=0
- enabled=1
- EOF
- yum clean all && yum makecache
- yum install -y mongo-10gen mongo-10gen-server
2.2 下载安装包安装
- cd /usr/local/
- wget -c https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.10.tgz #下载软件包
- tar -zxvf mongodb-linux-x86_64-rhel62-3.4.10.tgz
- ln -sv mongodb-linux-x86_64-rhel62-3.4.10 mongodb
- mkdir /usr/local/mongodb/{conf,mongoData,mongoLog} #创建数据,日志,配置文件目录
- touch /usr/local/mongodb/mongoLog/mongodb.log #创建日志文件
- echo "PATH=$PAHT:/usr/local/mongodb/bin">/etc/profile.d/mongodb.sh
- source etc/profile.d/mongodb.sh #配置环境变量
命令行启动:
- mongod --httpinterface --dbpath / usr / local / mongodb / mongoData --logpath / usr / local / mongodb / mongoLog / mongodb.log --fork
将启动的各项参数写入配置文件启动
- cat>/usr/local/mongodb/conf/mongodb.conf<<EOF
- dbpath=/usr/local/mongodb/mongoData #数据存储目录
- logpath=/usr/local/mongodb/mongoLog/mongodb.log #日志文件
- logappend=true # 使用追加的方式写日志
- journal=true
- quiet=true
- port=27017 #端口
- maxConns=20000 #最大连接数
- httpinterface=true #开启web界面端口为:28017
- fork=true #以守护进程的方式运行MongoDB
- EOF
- mongod -f /usr/local/mongodb/conf/mongodb.conf #指定配置文件启动
c
2.3 mongod配置参数详解
--quiet | # 安静输出 |
--port arg | # 指定服务端口号,默认端口27017 |
--bind_ip arg | # 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP |
--logpath arg | # 指定MongoDB日志文件,注意是指定文件不是目录 |
--logappend | # 使用追加的方式写日志 |
--pidfilepath arg | # PID File 的完整路径,如果没有设置,则没有PID文件 |
--keyFile arg | # 集群的私钥的完整路径,只对于Replica Set 架构有效 |
--unixSocketPrefix arg | # UNIX域套接字替代目录,(默认为 /tmp) |
--fork | # 以守护进程的方式运行MongoDB,创建服务器进程 |
--auth | # 启用验证 |
--cpu | # 定期显示CPU的CPU利用率和iowait |
--dbpath arg | # 指定数据库路径 |
--diaglog arg | # diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads |
--directoryperdb | # 设置每个数据库将被保存在一个单独的目录 |
--journal | # 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里 |
--journalOptions arg | # 启用日志诊断选项 |
--ipv6 | # 启用IPv6选项 |
--jsonp | # 允许JSONP形式通过HTTP访问(有安全影响) |
--maxConns arg | # 最大同时连接数 默认2000 |
--noauth | # 不启用验证 |
--nohttpinterface | # 关闭http接口,默认关闭27018端口访问 |
--noprealloc | # 禁用数据文件预分配(往往影响性能) |
--noscripting | # 禁用脚本引擎 |
--notablescan | # 不允许表扫描 |
--nounixsocket | # 禁用Unix套接字监听 |
--nssize arg (=16) | # 设置信数据库.ns文件大小(MB) |
--objcheck | # 在收到客户数据,检查的有效性, |
--profile arg | # 档案参数 0=off 1=slow, 2=all |
--quota | # 限制每个数据库的文件数,设置默认为8 |
--quotaFiles arg | # number of files allower per db, requires --quota |
--rest | # 开启简单的rest API |
--repair | # 修复所有数据库run repair on all dbs |
--repairpath arg | # 修复库生成的文件的目录,默认为目录名称dbpath |
--slowms arg (=100) | # value of slow for profile and console log |
--smallfiles | # 使用较小的默认文件 |
--syncdelay arg (=60) | # 数据写入磁盘的时间秒数(0=never,不推荐) |
--sysinfo | # 打印一些诊断系统信息 |
--upgrade | # 如果需要升级数据库 |
* Replicaton 参数
--fastsync | # 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步 |
--autoresync | # 如果从库与主库同步数据差得多,自动重新同步, |
--oplogSize arg | # 设置oplog的大小(MB) |
* 主/从参数
--master | # 主库模式 |
--slave | # 从库模式 |
--source arg | # 从库 端口号 |
--only arg | # 指定单一的数据库复制 |
--slavedelay arg | # 设置从库同步主库的延迟时间 |
* Replica set(副本集)选项:
--replSet arg | # 设置副本集名称 |
* Sharding(分片)选项
--configsvr | # 声明这是一个集群的config服务,默认端口27019,默认目录/data/configdb |
--shardsvr | # 声明这是一个集群的分片,默认端口27018 |
--noMoveParanoia | # 关闭偏执为moveChunk数据保存 |
三、基础操作
3.1 数据库操作
a.创建数据库
MongoDB 创建数据库的语法格式如下:
- use DATABASE_NAME #如果数据库不存在,则创建数据库,否则切换到指定数据库。
- use dbtest;
- > db
- dbtest
- > show dbs; #查看数据库
- admin 0.000GB
- dbtest 0.000GB
- local 0.000GB
b.删除数据库
MongoDB 删除数据库的语法格式如下:
- db.dropDatabase()
- > use dbtest;
- switched to db dbtest
- > db.dropDatabase()
- { "dropped" : "dbtest", "ok" : 1 }
- > show dbs;
- admin 0.000GB
- local 0.000GB
3.2 集合操作
a.创建集合
- db.createCollection(name, options)
- db.createCollection("user", autoindexID = true)
- > db.createCollection("user",autoindexID=true) #创建集合user
- { "ok" : 1 }
- > show collections; #查看集合
- user
b.删除集合
- db.collection.drop()
- > show collections;
- user
- > db.user.drop()
- true
- > show collections
3.3 文档操作
a.插入文档
MongoDB 使用 insert() 或 save() 方法向集合中插入文档
- db.COLLECTION_NAME.insert(document)
- > db.user.insert({name:"kaliarch",age:30}) #insert 插入文档
- WriteResult({ "nInserted" : 1 })
- > db.user.insert({name:"xuel",age:31})
- WriteResult({ "nInserted" : 1 })
- > db.user.save({name:"anchnet",age:34}) #save插入文档
- WriteResult({ "nInserted" : 1 })
- > db.user.find() #查看文档
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a870cceffdd1c65f633ef"), "name" : "xuel", "age" : 31 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
b.更新文档
MongoDB 使用 update() 和 save() 方法来更新集合中的文档
--------------------------------------update方式-----------------------------------------
- update()方式
- db.collection.update(
- <query>,
- <update>,
- {
- upsert: <boolean>,
- multi: <boolean>,
- writeConcern: <document>
- })
参数说明:
- > db.user.find()
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a870cceffdd1c65f633ef"), "name" : "xuel", "age" : 31 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
- > db.user.update({"name":"xuel"},{$set:{"name":"udxuel"}}) #更新name
- WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
- > db.user.find()
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a870cceffdd1c65f633ef"), "name" : "udxuel", "age" : 31 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
-----------------------------------------save方式--------------------------------------
- save()方式
- db.collection.save(
- <document>,
- {
- writeConcern: <document>
- })
参数说明:
- > db.user.find()
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a870cceffdd1c65f633ef"), "name" : "udxuel", "age" : 31 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
- > db.user.save({ "_id" : ObjectId("5a1a870cceffdd1c65f633ef"), "name" : "xuel", "age" : 31 }) #更新name
- WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
- > db.user.find()
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a870cceffdd1c65f633ef"), "name" : "xuel", "age" : 31 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
c.删除文档
remove() 方法的基本语法格式如下:
- db.collection.remove(
- <query>,
- {
- justOne: <boolean>,
- writeConcern: <document>
- })
参数说明:
- > db.user.find()
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a870cceffdd1c65f633ef"), "name" : "xuel", "age" : 31 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
- > db.user.remove({"name":"xuel","age":31}) #删除文档
- WriteResult({ "nRemoved" : 1 })
- > db.user.find()
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
d.查看文档
- db.collection.find(query, projection)
可以使用 pretty() 方法,语法格式如下:
- > db.user.find() { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"),
- "name" : "kaliarch",
- "age" : 30
- } { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"),
- "name" : "anchnet",
- "age" : 34
- } > db.user.find().pretty() { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"),
- "name" : "kaliarch",
- "age" : 30
- } { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"),
- "name" : "anchnet",
- "age" : 34
- }
3.3 条件操作符
条件操作符用于比较两个表达式并从mongoDB集合中获取数据
- > db.user.find()
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
- { "_id" : ObjectId("5a1a8ddeceffdd1c65f633f1"), "name" : "test", "age" : 50 }
- > db.user.find({"age":{$gt:33}}) #查询age大于33的文档
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
- { "_id" : ObjectId("5a1a8ddeceffdd1c65f633f1"), "name" : "test", "age" : 50 }
- > db.user.find({"age":{$lt:33}}) #查询age小于33的文档
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- > db.user.find({"age":{$eq:34}}) #查询age等于33的文档
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
- > db.user.find({"age":{$ne:10}}) 、#查询age不等于10的文档
- { "_id" : ObjectId("5a1a8704ceffdd1c65f633ee"), "name" : "kaliarch", "age" : 30 }
- { "_id" : ObjectId("5a1a8729ceffdd1c65f633f0"), "name" : "anchnet", "age" : 34 }
- { "_id" : ObjectId("5a1a8ddeceffdd1c65f633f1"), "name" : "test", "age" : 50
MongoDB基础
来源: http://www.bubuko.com/infodetail-2408019.html