转自http://zouqingyun.blog.51cto.com/782246/1676771
参考http://www.lanceyan.com/category/tech/mongodb
NOSQL的数据存储模型
键值模型:(key-avalue存储)
优点:查找速度快
缺点:数据无结构,通常只被当作字符串或二进制数据
应用场景:内容缓存
实例:Redis,Dynamo
列式模型
数据模型:数据按列存储,将同一列数据存在一起。
优点:查找迅速,可扩展性强,易于实现分布式
缺点:功能相对SQL很有限
应用场景:分布式文件系统或分布式存储
实例:Bigtable(google),Cassandra(facebook),HBase(hadoop),Hypertable
文档模型
数据模型:与键值模型类似,value指向结构化数据
优点:数据格式要求不严格,无需事先定义结构
缺点:查询性能不高,缺乏统一查询语法
应用场景:web应用
实例:MongoDB,CouchDB
图式模型
数据模型:图结构模型
优点:利用图结构相关算法提高性能,并满足特珠场景应用需求
缺点:难以实现分布式,功能有定向性
应用场景:社交网络,推荐系统,关系图谱
实例:Neo4j
一、环境
系统 CentOS6.4x64最小化安装
IP 192.168.3.32
二、安装mongodb
1.创建mongodb的yum源文件
[root@mongodb ~]# cat /etc/yum.repos.d/mongodb.repo [mongodb-org-3.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/ gpgcheck=0 enabled=1
2.安装mongodb
[root@mongodb ~]# yum install mongodb-org -y [root@mongodb ~]# mkdir -p /mongod/data [root@mongodb ~]# chown -R mongod.mongod /mongod/ #修改后的配置文件内容如下 [root@mongodb ~]# egrep -v "^$|^#" /etc/mongod.conf logpath=/var/log/mongodb/mongod.log logappend=true fork=true dbpath=/mongod/data pidfilepath=/var/run/mongodb/mongod.pid bind_ip=192.168.3.32
3.启动mongodb
[root@mongodb ~]# /etc/init.d/mongod start Starting mongod: [ OK ] [root@mongodb ~]# netstat -tunlp |grep mongo tcp 0 0 192.168.3.32:27017 0.0.0.0:* LISTEN 1545/mongod
三、操作mongodb
基础操作,使用mongodb命令
[root@mongodb ~]# mongo mongo: /usr/lib64/libssl.so.10: no version information available (required by mongo) mongo: /usr/lib64/libcrypto.so.10: no version information available (required by mongo) mongo: /usr/lib64/libcrypto.so.10: no version information available (required by mongo) #有报错,缺少依赖包 [root@mongodb ~]# yum install openssl -y #连接到mongodb上,有警告信息 [root@mongodb ~]# mongo --host 192.168.3.32 MongoDB shell version: 3.0.5 connecting to: 192.168.3.32:27017/test Server has startup warnings: 2015-08-05T10:42:04.020+0800 I CONTROL [initandlisten] 2015-08-05T10:42:04.020+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1024 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files. > #解决如下 [root@mongodb ~]# tail -4 /etc/security/limits.conf mongod soft nofile 64000 mongod hard nofile 64000 mongod soft nproc 32000 mongod hard nproc 32000 #重启mongodb服务,再次连接mongodb [root@mongodb ~]# /etc/init.d/mongod restart Stopping mongod: [ OK ] Starting mongod: [ OK ] [root@mongodb ~]# mongo --host 192.168.3.32 MongoDB shell version: 3.0.5 connecting to: 192.168.3.32:27017/test >
help命令,查看所有命令帮助
> help db.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> set current database db.foo.find() list objects in collection foo db.foo.find( { a : 1 } ) list objects in foo where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell #查询数据库 > show dbs local 0.078GB
创建一个集合
> for(i=1;i<=100;i++) db.testcoll.insert({Name: "User"+i,Age: i,Gender: "M",PerferBook: ["first book","second book"]}) WriteResult({ "nInserted" : 1 }) #查询集合testcoll #说明:查询集合testcoll,使用find方法,只显示前5行 > db.testcoll.find().limit(5) { "_id" : ObjectId("55c17bcaf9d080237120b434"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b435"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b436"), "Name" : "User3", "Age" : 3, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b437"), "Name" : "User4", "Age" : 4, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b438"), "Name" : "User5", "Age" : 5, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } #查看Name=User55的数据 > db.testcoll.find({Name: "User55"}) { "_id" : ObjectId("55c17bcbf9d080237120b46a"), "Name" : "User55", "Age" : 55, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } #删除Name=User14的数据 > db.testcoll.remove({Name: "User14"}) WriteResult({ "nRemoved" : 1 }) > db.testcoll.find({Name: "User14"}) #从结果能看到该数据已被删除 > db.testcoll.find().limit(14) { "_id" : ObjectId("55c17bcaf9d080237120b434"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b435"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b436"), "Name" : "User3", "Age" : 3, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b437"), "Name" : "User4", "Age" : 4, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b438"), "Name" : "User5", "Age" : 5, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b439"), "Name" : "User6", "Age" : 6, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b43a"), "Name" : "User7", "Age" : 7, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b43b"), "Name" : "User8", "Age" : 8, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b43c"), "Name" : "User9", "Age" : 9, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b43d"), "Name" : "User10", "Age" : 10, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b43e"), "Name" : "User11", "Age" : 11, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b43f"), "Name" : "User12", "Age" : 12, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b440"), "Name" : "User13", "Age" : 13, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b442"), "Name" : "User15", "Age" : 15, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] }
更新数据
#将Name=User12的数据的Age更新为32 > db.testcoll.update({Name: "User12"},{$set: {Age: 32}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) #查询结果 > db.testcoll.find({Name: "User12"}) { "_id" : ObjectId("55c17bcbf9d080237120b43f"), "Name" : "User12", "Age" : 32, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } #将Age>=95的数据的Gender字段更新成“F” > db.testcoll.update({Age: {$gte:95}},{$set: {Gender: "F"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) #查询结果,能看到只更新了第一条 > db.testcoll.find({Age: {$gte: 95}},{Name:1,Gender:1}) { "_id" : ObjectId("55c17bcbf9d080237120b492"), "Name" : "User95", "Gender" : "F" } { "_id" : ObjectId("55c17bcbf9d080237120b493"), "Name" : "User96", "Gender" : "M" } { "_id" : ObjectId("55c17bcbf9d080237120b494"), "Name" : "User97", "Gender" : "M" } { "_id" : ObjectId("55c17bcbf9d080237120b495"), "Name" : "User98", "Gender" : "M" } { "_id" : ObjectId("55c17bcbf9d080237120b496"), "Name" : "User99", "Gender" : "M" } { "_id" : ObjectId("55c17bcbf9d080237120b497"), "Name" : "User100", "Gender" : "M" } #批量更新 > db.testcoll.update({Age: {$gte:95}},{$set: {Gender: "F"}},{multi:true}) WriteResult({ "nMatched" : 6, "nUpserted" : 0, "nModified" : 5 }) #结果能看到数据已经全部被更新 > db.testcoll.find({Age: {$gte:95}},{Name:1,Gender:1}) { "_id" : ObjectId("55c17bcbf9d080237120b492"), "Name" : "User95", "Gender" : "F" } { "_id" : ObjectId("55c17bcbf9d080237120b493"), "Name" : "User96", "Gender" : "F" } { "_id" : ObjectId("55c17bcbf9d080237120b494"), "Name" : "User97", "Gender" : "F" } { "_id" : ObjectId("55c17bcbf9d080237120b495"), "Name" : "User98", "Gender" : "F" } { "_id" : ObjectId("55c17bcbf9d080237120b496"), "Name" : "User99", "Gender" : "F" } { "_id" : ObjectId("55c17bcbf9d080237120b497"), "Name" : "User100", "Gender" : "F" }
统计数据
> db.testcoll.count() 99
条件查询,查询Age>=96的数据
#指定条件Age进行查询 > db.testcoll.find({Age: {$gte:96}}) { "_id" : ObjectId("55c17bcbf9d080237120b493"), "Name" : "User96", "Age" : 96, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b494"), "Name" : "User97", "Age" : 97, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b495"), "Name" : "User98", "Age" : 98, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b496"), "Name" : "User99", "Age" : 99, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } { "_id" : ObjectId("55c17bcbf9d080237120b497"), "Name" : "User100", "Age" : 100, "Gender" : "M", "PerferBook" : [ "first book", "second book" ] } #查询Age>=96的数据,只显示Name和Age > db.testcoll.find({Age: {$gte:96}},{Name:1,Age:1}) { "_id" : ObjectId("55c17bcbf9d080237120b493"), "Name" : "User96", "Age" : 96 } { "_id" : ObjectId("55c17bcbf9d080237120b494"), "Name" : "User97", "Age" : 97 } { "_id" : ObjectId("55c17bcbf9d080237120b495"), "Name" : "User98", "Age" : 98 } { "_id" : ObjectId("55c17bcbf9d080237120b496"), "Name" : "User99", "Age" : 99 } { "_id" : ObjectId("55c17bcbf9d080237120b497"), "Name" : "User100", "Age" : 100 }
插入一条User101的数据
#插入数据 > db.testcoll.insert({Name: "User101",Age: 101,Gender: "F",Address: "guangzhou"}) WriteResult({ "nInserted" : 1 }) #查询结果 > db.testcoll.find({Name: "User101"}) { "_id" : ObjectId("55c18169f9d080237120b498"), "Name" : "User101", "Age" : 101, "Gender" : "F", "Address" : "guangzhou" } #在集合中找出包含Address字段的数据 > db.testcoll.find({Address: {$exists: true}}) { "_id" : ObjectId("55c18169f9d080237120b498"), "Name" : "User101", "Age" : 101, "Gender" : "F", "Address" : "guangzhou" }