MongDB是开源的document型数据库(非关系型数据库)支持NOSQL,是用C++编写的,但他的结构是最类似于关系型数据库的,支持K-V存储。
为什么选择mongdb?
第一它存储的内容为对象和JSON格式
第二可以在任何属性上加索引;
第三 可复制并支持自动故障转移,自动恢复等功能,使其具有高可用性;(待补全)
第四 自动分片(待补全)
下为MongDB存储内容需要注意的几点(不局限于这几点)
键是字符串UTF-8字符串(少数例外);键不能空字符串\0,这个字符串表示键的结尾。
(1)键值区分大小写 {“foo”:123}不同于{“Foo”:123}
(2)值区分类型{“foo”:123}不同于{“foo”: “123”}
(3)键不能为空;键不能重复
(4)键值是有序的{“x”:1, “y”:2}不同于{“y”:2, “x”:1}1.MongoDB安装部署
(1)brew installmongodb
我本地的安装路径为供参考:/usr/local/Cellar/mongodb/3.4.9/bin安装后的配置文件路径为:/usr/local/etc/mongod.conf,内容如下所示:
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1
(2)创建目录 mkdir-p /data/db 并为其赋予权限 chown `id -u` /data/db,这个应该是用来存储数据文件的,里面包括索引文件和collection文件内容
(3)需要配环境变量,不然启动时需要进入到mongodb的bin路径下执行。环境变量配置如下所示:
vi ~/.bash_profile 加入如下一行:export PATH="/usr/local/Cellar/mongodb/3.4.9/bin:$PATH"
启动命令为:mongod
(4)进入shell命令模式 便可持行mongo命令了并支持jS交互。命令为:mongo
2.常用命令总结
(1)查看相关
- mongod :启动mongdb
- mongo: 开启Mongo的shell命令
- > showdbs :查看用多少个数据库
- > show collections:查看所有集合
- > db:查看当前使用DB
- > usemydb:切换DB,如不丰会自动创建
- db.collectionName.find().pretty()注:pretty()显示可以格式化显示
(2) 增加相关
db.createCollection(“zltest”):创建collection(相当于创建表)
db.zltest1.insert({"title":"engineer","dept":21,"group":"XXAQ"}):创建一个集合并为其存放文档,如果集合存在则写入,不存在会自动创建
db.createCollection("mycol",{ capped : true, autoIndexId : true, size : 6142800, max : 10000 } )
注:上述中参数说明:capped:Boolean[true自动扩容collection];autoIndexId:Boolean[true在_id上创建索引默认为false]; size:number[配合capped=true使用指collection的最大值];max:number[指指documents的最大值]
以下为增加document内容示例:
db.mongTest.update({"name":"zhanglu"},{$set:{"name":"zhanglu888"}})
- 注:修改还可以用save方法同时可以保存多条,insert和update的区别在于,update存在则修改,不存在增加;insert只用于增加操作
(4)删除document
db.mongTest.remove({"name":"zhanglu888"})
(5)复杂查询总结
- 按列查询:db.zltest.find({},{"id":1,"name":1,_id:0}) ,使用按列查询时需要注意_id总会显示,所以需要_id:0指定不显示
{ "name" : "zl" }
{ "id" : 1 }
{ "id" : 2 }
{ "id" : 3 }
{ "id" : 4 }
{ "id" : 5 }
{ "name" : "test0", "id" : 0 }
{ "name" : "test1", "id" : 1 }
{ "name" : "test2", "id" : 2 }
- 条件查询【where】:
db.zltest.find()
{ "_id" : ObjectId("59cb36db25238f9e9cb9774f"), "name" : "zl", "age" : 33, "no" : "03130321" }
{ "_id" : ObjectId("59cc9beecd12a63a4befaba8"), "title" : "mongoDB", "id" : 1, "likes" : 100, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9db"), "title" : "mongoDB1", "id" : 2, "likes" : 110, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dc"), "title" : "mongoDB2", "id" : 3, "likes" : 210, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dd"), "title" : "mongoDB3", "id" : 4, "likes" : 310, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9de"), "title" : "mongoDB4", "id" : 5, "likes" : 410, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdf6815f77c2c6c7f327ae"), "name" : "test0", "id" : 0 }
{ "_id" : ObjectId("59cdf6815f77c2c6c7f327af"), "name" : "test1", "id" : 1 }
{ "_id" : ObjectId("59cdf6815f77c2c6c7f327b0"), "name" : "test2", "id" : 2 }
where key=value ---- {key:value} 注:默认_id是显示的,如下查询可以不查询_id
db.zltest.find({},{"id":1,"name":1,_id:0})
{ "name" : "zl" }
{ "id" : 1 }
{ "id" : 2 }
{ "id" : 3 }
{ "id" : 4 }
{ "id" : 5 }
{ "name" : "test0", "id" : 0 }
{ "name" : "test1", "id" : 1 }
{ "name" : "test2", "id" : 2 }
where key<value ---- {key:{$lt:value}}
示例内容略。。。。
where key<=value ---- {key:{$lte:value}}
db.zltest.find({"likes":{$lte:110}})
{ "_id" : ObjectId("59cc9beecd12a63a4befaba8"), "title" : "mongoDB", "id" : 1, "likes" : 100, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9db"), "title" : "mongoDB1", "id" : 2, "likes" : 110, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
key>value ---- {key:{$gt:value}}
示例内容略。。。
key>=value ---- {key:{$gte:value}}
db.zltest.find({"likes":{$gte:110}})
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9db"), "title" : "mongoDB1", "id" : 2, "likes" : 110, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dc"), "title" : "mongoDB2", "id" : 3, "likes" : 210, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dd"), "title" : "mongoDB3", "id" : 4, "likes" : 310, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9de"), "title" : "mongoDB4", "id" : 5, "likes" : 410, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
key!=value ---- {key:{$ne:value}}
db.zltest.find({"title":{$ne:"mongoDB"}})
{ "_id" : ObjectId("59cb36db25238f9e9cb9774f"), "name" : "zl", "age" : 33, "no" : "03130321" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9db"), "title" : "mongoDB1", "id" : 2, "likes" : 110, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dc"), "title" : "mongoDB2", "id" : 3, "likes" : 210, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dd"), "title" : "mongoDB3", "id" : 4, "likes" : 310, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9de"), "title" : "mongoDB4", "id" : 5, "likes" : 410, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdf6815f77c2c6c7f327ae"), "name" : "test0", "id" : 0 }
{ "_id" : ObjectId("59cdf6815f77c2c6c7f327af"), "name" : "test1", "id" : 1 }
{ "_id" : ObjectId("59cdf6815f77c2c6c7f327b0"), "name" : "test2", "id" : 2 }
key1=value1 and key2=value2 ---- {$and:[{key1:value1},{key2:value2}]}
db.zltest.find({$and:[{"url":"http://www.tutorialspoint.com/mongodb/"},{"likes":210}]})
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dc"), "title" : "mongoDB2", "id" : 3, "likes" : 210, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
key1=value1 and(key2=value2 or key3=value3) ---- {$or:[{key1:value1},{key2:value2}]}
db.zltest.find({"likes":210,$or:[{"likes":310},{"likes":210}]})
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dc"), "title" : "mongoDB2", "id" : 3, "likes" : 210, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
- 其它函数sort()、skip()、limit()
db.zltest.find().skip(6).limit(2).sort({"id":1})
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dc"), "title" : "mongoDB2", "id" : 3, "likes" : 210, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
{ "_id" : ObjectId("59cdb54ea30515a6bffcf9dd"), "title" : "mongoDB3", "id" : 4, "likes" : 310, "description" : "database", "url" : "http://www.tutorialspoint.com/mongodb/", "by" : "tutorials point" }
- 分组函数aggregate ,用法格式为 db.collectionName.aggregate([{$group:{"columnName":"$columnName","columnName1":{$sum:"columnName1"}}}]) 注:如果查询总记录数时,$sum:1即可,以下为示例参考
已知数据内容如下所示:db.mongTest.find()
{ "_id" : ObjectId("59ce043a5f77c2c6c7f327b1"), "id" : 0, "name" : "zl", "salary" : 0 }
{ "_id" : ObjectId("59ce043a5f77c2c6c7f327b2"), "id" : 1, "name" : "zl", "salary" : 1000 }
{ "_id" : ObjectId("59ce043a5f77c2c6c7f327b3"), "id" : 2, "name" : "zl", "salary" : 2000 }
{ "_id" : ObjectId("59ce04575f77c2c6c7f327b4"), "id" : 3, "name" : "xxn", "salary" : 6000 }
{ "_id" : ObjectId("59ce04575f77c2c6c7f327b5"), "id" : 4, "name" : "xxn", "salary" : 8000 }
{ "_id" : ObjectId("59ce04575f77c2c6c7f327b6"), "id" : 5, "name" : "xxn", "salary" : 10000 }
b.mongTest.aggregate([{$group:{_id:"$name",totalSalary:{$sum:"$salary"},num:{$sum:1}}}])
{ "_id" : "xxn", "totalSalary" : 24000, "num" : 3 }
{ "_id" : "zl", "totalSalary" : 3000, "num" : 3 }
- exists 和 in的使用方法
db.zltest.find({title:{$exists:false}})
{ "_id" : ObjectId("59cb36db25238f9e9cb9774f"), "name" : "zl", "age" : 33, "no" : "03130321" }{ "_id" : ObjectId("59cdf6815f77c2c6c7f327ae"), "name" : "test0", "id" : 0 }{ "_id" : ObjectId("59cdf6815f77c2c6c7f327af"), "name" : "test1", "id" : 1 }{ "_id" : ObjectId("59cdf6815f77c2c6c7f327b0"), "name" : "test2", "id" : 2 }
db.zltest.find({name:{$in:["zl"]}})
{ "_id" : ObjectId("59cb36db25238f9e9cb9774f"), "name" : "zl", "age" : 33, "no" : "03130321" }