您的位置:首页 > 数据库 > Mongodb

NoSQL——MongoDB基础

2018-08-28 17:44 218 查看
一. MongoDB介绍

1. 简述

介于关系数据库和非关系数据库之间

分布式文件存储数据库,web应用可扩展的高性能数据存储解决方案

数据存储为文档(类似于JSON),数据结构由键值对组成

可设置任何属性的索引,支持副本,分片

2. 对比

MongoDB MySQL/RDBMS
DB(库) Databases(数据库)
Collection(集合)Table(表)
Document(文档)Row/Record(行/记录)
Field(字段)Col(列)
IndexIndex
Embedding&LinkdingJoin
ShardPartition
Sharding KeysPartition Key
二. 搭建MongoDB服务器

1. 装包

mkdir /usr/local/mongodb  #创建工作目录
tar -zxf mongodb-linux-x86_64-rhel70-3.6.3.tgz  #免安装,解压即可使用

cd mongodb-linux-x86_64-rhel70-3.6.3/
cp -r bin /usr/local/mongodb/  #将bin目录复制到工作目录
cd /usr/local/mongodb/
mkdir -p etc log data/db  #创建数据库目录

2. 创建配置文件(手动创建)

vim /usr/local/mongodb/etc/mongodb.conf
logpath=/usr/local/mongodb/log/mongodb.log  #日志路径
logappend=true                              #追加方式写日志
dbpath=/usr/local/mongodb/data/db           #数据库目录
fork=true                                   #守护进程方式运行

3. 启动/停止服务

#启动服务
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf
#停止服务
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf  --shutdown


由于命令很长,可以定义别名:

vim /root/.bashrc
alias mstart='/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf '
alias mstop='/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf  --shutdown'

mstart #启动服务
mstop  #停止服务


查看进程:

ps -C mongod


查看端口:

ss -antup | grep 27017

4. 连接服务

/usr/local/mongodb/bin/mongo


命令过长,创建软连接:

ln -s /usr/local/mongodb/bin/mongo /root/bin
#注意:源地址必须是绝对路径,不然会报错!!!

5. 修改ip地址和端口

mstop

vim /usr/local/mongodb/etc/mongodb.conf
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
dbpath=/usr/local/mongodb/data/db
fork=true
port=27050           #修改端口号
bind_ip=192.168.4.50 #修改登陆ip地址

mstart
mongo --host 192.168.4.50 --port 27050  #修改后登陆时需要增加参数

三. MongoDB基本使用

【注意】严格区分字母大小写!!!

1. 数据库管理

(1) 基本操作

show dbs //查看已有的库

db //查看当前所在的库

use 库名 //切换库,若不存在则延时创建

db.dropDatabase() //删除当前所在的库

(2) 命名规范

不能时空字符串("")

不能含有空格 . $ / \ \0

应全部小写

最多64字节

2. 集合管理

(1) 基本操作

show collections或show tables //查看库下所有的集合

db.集合名.drop() //删除集合

db.集合名.save({"",""}) //创建集合,集合不存在时则创建并添加文档

(2) 集合命名规范

不能时空字符串(“”)

不能含有\0,此字符表示集合的结尾

不能以system.开头,这是为系统集合保留的前缀

不能含有保留字符

3. 文档管理

db.集合名.find()

db.集合名.count()

db.集合名.insert({name:"alex"})

db.集合名.find({name:"alex"},{_id:0})

db.集合名.findOne()

db.集合名.remove({}) //删除所有文档

db.集合名.remove({name:"alex"}) //删除匹配的文档

db.aaa.insert({name:"bob",age:24})
db.aaa.insert({name:"jack",tel:"156324"})
db.aaa.insert({name:"tom",home:"beijing"})

db.aaa.count()  #输出3
db.aaa.find()
#输出
{ "_id" : ObjectId("5b84eb90a678971a18f2312e"), "name" : "bob", "age" : 24 }
{ "_id" : ObjectId("5b84eb9ba678971a18f2312f"), "name" : "jack", "tel" : "156324" }
{ "_id" : ObjectId("5b84eba9a678971a18f23130"), "name" : "tom", "home" : "beijing" }
db.aaa.find({name:"bob"},{_id:0})
#输出{ "name" : "bob", "age" : 24 }
db.aaa.findOne()
#输出{ "_id" : ObjectId("5b84eb90a678971a18f2312e"), "name" : "bob", "age" : 24 }

db.aaa.remove({home:"beijing"})
#输出WriteResult({ "nRemoved" : 1 })
db.aaa.find()
#输出
{ "_id" : ObjectId("5b84eb90a678971a18f2312e"), "name" : "bob", "age" : 24 }
{ "_id" : ObjectId("5b84eb9ba678971a18f2312f"), "name" : "jack", "tel" : "156324" }

四. 基本数据类型

1. 字符串:utf-8字符串都可以表示为字符串类型 {name:"alex"}

2. 布尔值:true/false {aaa:true}

3. 空值:null {bbb:null}

4. 数值:默认使用64位浮点型数值。 {x: 3.14}或{x:10}

NumberInt(4字节整数) {x:NumberInt(5)}

NumberLong(6字节整数) {x:NumberLong(5)}

5.属组:数据列表 {x:["a","b","c"]}

6.代码:{x:function(){/*代码段*/}}

7. 日期: {x:new Date()}

8. 对象:对象id是一个12字节的字符串,是文档的为一标识。 {s:ObjectId()}

9.内嵌(相当于Redis的hash): 文档的嵌套 {frank:{tel:"12345", addr:beijing}}

10. 正则 {x:/正则表达式/}

db.aaa.insert({name:"tom",boy:true})
db.aaa.insert({name:"jack",marry:null})
db.aaa.insert({name:"bob",age:25.5})
db.aaa.insert({name:"bob",age:NumberInt(25)})
db.aaa.insert({name:"lucy",hobby:["movie","sing","tennis"]})
db.aaa.insert({name:"php",phpcode:function(){/* <?php echo "aaa" ?> */}})
db.aaa.insert({name:"alex",birth:new Date()})
db.aaa.insert({name:"kkk",new_id:ObjectId()})
db.aaa.insert({mmm:{tel:"213234",home:"shenzhen"}})
db.aaa.insert({name:"nnn",regex:/^.*&/})

db.aaa.find()
{ "_id" : ObjectId("5b84eca4a678971a18f23131"), "name" : "tom", "boy" : true }
{ "_id" : ObjectId("5b84ecb8a678971a18f23132"), "name" : "jack", "marry" : null }
{ "_id" : ObjectId("5b84eccba678971a18f23133"), "name" : "bob", "age" : 25.5 }
{ "_id" : ObjectId("5b84ecdba678971a18f23134"), "name" : "bob", "age" : 25 }
{ "_id" : ObjectId("5b84ed10a678971a18f23135"), "name" : "lucy", "hobby" : [ "movie", "sing", "tennis" ] }
{ "_id" : ObjectId("5b84ed3ca678971a18f23136"), "name" : "php", "phpcode" : { "code" : "function (){/* <?php echo \"aaa\" ?> */}" } }
{ "_id" : ObjectId("5b84ed57a678971a18f23137"), "name" : "alex", "birth" : ISODate("2018-08-28T06:36:07.427Z") }
{ "_id" : ObjectId("5b84ed6ea678971a18f23139"), "name" : "kkk", "new_id" : ObjectId("5b84ed6ea678971a18f23138") }
{ "_id" : ObjectId("5b84ed96a678971a18f2313a"), "mmm" : { "tel" : "213234", "home" : "shenzhen" } }
{ "_id" : ObjectId("5b84edb7a678971a18f2313b"), "name" : "nnn", "regex" : /^.*&/ }

五. 数据的导入导出

1. 数据的导出

mkdir /mbak  #创建导出的目录
ln -s /usr/local/mongodb/bin/mongoexport  /root/bin/  #创建mongoexport命令的软连接

格式1:

mongoexport [ --host IP地址 --port 端口 ] \

-d 库名 -c 集合名 [ - q '{条件}' ] -f 字段1,字段2... \

--type=csv > 目录名/文件名.csv

#导出name和tel两个字段
mongoexport --host 192.168.4.50 --port 27050 -d alex -c aaa --type=csv -f name,tel > /mbak/aaa.bak

格式2:

mongoexport [ --host IP地址 --port 端口 ] \

-d 库名 -c 集合名 [ -q '{条件}' -f 字段1,字段2... ] \

--type=json > 目录名/文件名.json

#导出所有记录
mongoexport --host 192.168.4.50 --port 27050 -d alex -c aaa --type=json  > /mbak/aaa.json
#导出所有包含name字段的记录
mongoexport --host 192.168.4.50 --port 27050 -d alex -c aaa -q '{name:/.*/}' --type=json  > /mbak/bbb.json

2. 数据导入

ln -s /usr/local/mongodb/bin/mongoimport  /root/bin/  #创建mongoimport命令的软连接

格式1:

mongoimport [ --host IP地址 --port 端口 ] \

-d 库名 -c 集合名 \

--type=json 目录名/文件名.json

#将aaa.json的数据导入indb库的user表
mongoimport --host 192.168.4.50 --port 27050 -d indb -c user --type=json /mbak/aaa.json

格式2:

mongoimport [ --host IP地址 --port 端口 ] \

-d 库名 -c 集合名 [-f 字段1,字段2... ] \

--type=csv [ --headerling ] [ --drop ] 目录名/文件名.csv

导入数据时,若库和集合不存在,则先创建

若库和集合已存在,则追加导入

--drop选项可以删除原数据后导入新数据

--header 忽略标题

mongoimport --host 192.168.4.50 --port 27050 -d indb -c csvuser --type=csv -f aaa,bbb ./aaa.csv
#输出
db.csvuser.find()
{ "_id" : ObjectId("5b84fe37e4d25d3d040fb719"), "name" : "karl", "sex" : "boy" }
{ "_id" : ObjectId("5b8501eefd53279a83f210a8"), "aaa" : "name", "bbb" : "tel" }
{ "_id" : ObjectId("5b8501eefd53279a83f210a9"), "aaa" : "tom", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210aa"), "aaa" : "jack", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210ab"), "aaa" : "bob", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210ac"), "aaa" : "bob", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210ad"), "aaa" : "lucy", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210ae"), "aaa" : "php", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210af"), "aaa" : "alex", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210b0"), "aaa" : "kkk", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210b1"), "aaa" : "", "bbb" : "" }
{ "_id" : ObjectId("5b8501eefd53279a83f210b2"), "aaa" : "nnn", "bbb" : "" }

mongoimport --host 192.168.4.50 --port 27050 -d indb -c csvuser --type=csv --headerline --drop ./aaa.bak
#输出
db.csvuser.find()
{ "_id" : ObjectId("5b850215fd53279a83f210c0"), "name" : "tom", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c1"), "name" : "jack", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c2"), "name" : "bob", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c3"), "name" : "bob", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c4"), "name" : "lucy", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c5"), "name" : "php", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c6"), "name" : "alex", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c7"), "name" : "kkk", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c8"), "name" : "", "tel" : "" }
{ "_id" : ObjectId("5b850215fd53279a83f210c9"), "name" : "nnn", "tel" : "" }

六. 数据的备份恢复

1. 数据备份

ln -s /usr/local/mongodb/bin/mongodump  /root/bin/     #创建mongodump命令的软连接
ln -s /usr/local/mongodb/bin/bsondump  /root/bin/      #创建bsondump命令的软连接
ln -s /usr/local/mongodb/bin/mongorestore  /root/bin/  #创建mongorestore命令的软连接

格式1: 备份到当前目录下
3ff0
的dump目录下

mongodump [ --host IP地址 --port 端口 ]

格式2:指定备份的库和目录

mongodump [ --host IP地址 --port 端口 ] -d 数据库名 -c 集合名 -o 目录名

#备份整个数据库到当前目录下的dump目录
mongodump --host 192.168.4.50 --port 27050
#备份alex库的集合aaa到根目录下
mongodump --host 192.168.4.50 --port 27050 -d alex -c aaa -o /

#查看aaa集合
bsondump /alex/aaa.bson

2. 数据恢复

库和集合不存在时,则创建再恢复

恢复时不一定要恢复到原库和原集合中

格式:mongorestore [ --host IP地址 --port 端口 ] -d 数据库名 [ -c 集合名 ] 备份目录名

#将alex库恢复为newdb库
mongorestore --host 192.168.4.50 --port 27050 -d newdb /mbak/dump/alex/
#将alex库的aaa集合恢复为ddbb库的ttt集合
mongorestore --host 192.168.4.50 --port 27050 -d ddbb -c ttt /mbak/dump/alex/aaa.bson
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  NoSQL MongoDB