superwensuperwen@gmail.comMongoDB培训第一部分MongoDB简介23MongoDB特性MongoDB是一个可扩展、高性能的下一代数据库,它的特点是高性能、易部署、易使用、存储数据非常方便,主要特性有:1、面向文档存储,json格式的文档易读,高效。2、模式自由,支持动态查询、完全索引,无模式。3、高效的数据存储,效率提高。4、支持复制和故障恢复。5、以支持云级别的伸缩性,支持水平的数据库集群,可动态添加额外的服务器4MongoDB工作方式传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB同样也是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。文档类似于json的键值对。{“name”:”jone”,”age”:13}集合一组文档的集合。一个集合下的稳定无模式限制。提问:既然是这样为什么还会有多个集合?注意:集合命名不能为空,\0,不能以system.开头,不能含有$数据库命名小写不能含有空格,$\/\0等5•在32位系统上,不支持大于2.5G的数据。•单个文档大小限制为16M•锁粒度太粗,MongoDB使用的是一把全局的读写锁,详见这里•不支持join操作和事务机制,这个确实是非MongoDB要做的领域•对内存要求比较大,至少要保证热数据(索引,数据及系统其它开销)都能装进内存•用户权限方面比较弱,将机器部署在安全的内网环境中,尽量不要用权限•MapReduce在单个实例上无法并行,可用Auto-Sharding实现。是由JS引擎的限制造成的。•MapReduce的结果无法写入到一个被Sharding的Collection中,2.0版本对这个问题的解决好像也不彻底•对于数组型的数据操作不够丰富MongoDB的局限与不足6MongoDB支持的语言7谁在使用MongoDB8null布尔ture|false整数123浮点12.3字符串“helloworld”对象ID用newObjectId()来申明。日期用newDate()来申明时间戳数组[“apple”,”blanan”,”pear”]内嵌文档{“username”:“jone”,“age”:13,“contact”:{“home”:”123”,”moblie”:”456”}}RegExp正则表达式/[a-z]/MongoDB数据类型9MongoDB的ObjectId不同的机器都能用全局唯一的同种方法方便的生成它。ObjectId使用12字节的存储空间,其生成方式如下:4e931cb6edcd881e1900017f时间戳机器IDPID计数器时间戳保证秒级唯一,机器ID保证设计时考虑分布式,避免时钟同步,PID保证同一台服务器运行多个mongod实例时的唯一性,最后的计数器保证同一秒内的唯一性。第二部分MongoDB安装1011下载:MongoDB的官网:根据需要下载windows还是linux版本,是32位还是64位。$curl启动数据库进程--dbpath指定数据库的目录--port指定数据库的端口,默认是27017--bind_ip绑定IP--directoryperdb为每个db创建一个独立子目录--logpath指定日志存放目录MongoDB安装12$./mongo$./mongo192.168.10.71/epg(不填,连接本机test数据库)MongoDBshellversion:2.0.1connectingto:192.168.10.71/epgtypehelpforhelpMongoDB客户端工具第三部分MongoDB基本操作1314MongoDB基本操作创建数据库当use的时候,系统就会自动创建一个数据库。如果use之后没有创建任何集合。系统就会删除这个数据库。创建集合同样,当插入一个文档的时候,一个集合就会自动创建。添加一个用户db.addUser(“portal”,”portalpass”)添加一个认证db.auth(“portal”,”portalpass”)在用户数据库中添加的用户和认证只能管理用户自己的数据库。在admin数据库中创建的用户,可以管理所有数据库。1516db.user.insert({_id:10,name:'tom',age:20});db.user.update({_id:0},{$set:{age:20}});//id为0的文档age改为20db.user.update({_id:0},{$inc:{age:1}});//id为0的文档age自加1db.user.update({_id:0},{$unset:{sex:1}});//删除给定的字段fielddb.user.update({_id:0},{$push:{aihao:'football'}});//aihao得是个数组db.user.update({_id:0},{$pop:{aihao:n}});//删除数组中的第n个元素db.user.update({_id:0},{$pull:{aihao:'bike'}});db.user.update({_id:0},{$rename:{'aihao':'ah'}});MongoDBshell增删改查17db.user.findOne();//查询db.user.findOne({name:p.author});//带参数查询db.user.find({},{age:0});//返回除了age字段外的所有字段db.user.find({userid:16},{name:1});//返回userid=16的name字段db.user.find({age:{$gt:12}});//$gt大于db.user.find({age:{$lt:12}});//$lt小于db.user.find({age:{$gte:12}});//大于等于db.user.find({age:{$gte:12}});//小于等于db.user.find({_id:{$ne:0}});//不等于db.user.find({_id:{$gt:5,$lte:10}});//区间查询MongoDBshell增删改查18db.user.find({a:{$all:[1,2,3,4]}});//all全部满足db.user.find({_id:{$in:[2,3,4,5,6]}});//indb.user.find({_id:{$nin:[1,2,3,4]}});//nin跟$in操作相反db.user.find({userid:{$exists:true}});//字段存在db.user.find(this._id==1);//moddb.user.find({_id:{$mod:[10,1]}});//同上db.user.find({$or:[{_id:2},{name:'user3'},{userid:4}]})db.user.find({$nor:[{_id:2},{name:'user3'},{userid:4}]})db.user.find({a:{$size:3}});//查询数组长度等于输入参数的数组db.user.find({a:{$type:2}});//按文档查询db.user.find({name:/u.*4$/i});//正则表达式MongoDBshell增删改查19db.user.find({}).sort({last_name:1});//排序db.user.distinct(zip-code);//distinctdb.user.find().skip(10);//跳过前10条db.user.find().limit(8);//limitdb.user.find().skip(20).limit(10);//db.user.find({},{},10,20);//同上db.user.find().count();//统计db.posts.find({},{comments:{$slice:5}});//前5条评论db.posts.find({},{comments:{$slice:-5}});//后5条评论db.posts.find({},{comments:{$slice:[20,10]}});//skip20,limit10db.posts.find({},{comments:{$slice:[-20,10]}});//20fromend,limit10MongoDBshell增删改查20db.stu.remove({_id:17});//删除db.persons.ensureIndex({name:1});//建立索引db.things.ensureIndex({address.city:1})//嵌入式索引,也可以是一个子文档db.things.ensureIndex({j:1,name:-1});//组合索引db.things.ensureIndex({firstname:1,lastname:1},{unique:true});//唯一索引db.persons.getIndexes();//查看索引db.collection.dropIndexes();//删除所有索引db.collection.dropIndex({x:1,y:-1});//删除单个索引db.myCollection.reIndex();//重建索引MongoDBshell增删改查21db.addUser(theadmin,anadminpassword);//添加用户db.addUser(guest,passwordForGuest,true);//添加只读用户db.auth(theadmin,anadminpassword);//添加认证db.system.users.find();//查看用户db.removeUser(username);//删除用户db.system.users.remove({user:username});//同上MongoDBshell增删改查22db.user.stats//查看collection的状态db.user.drop()dropthecollectiondb.cloneDatabase(fromhost)//克隆数据db.copyDatabase(fromdb,todb,fromhost)//复制数据库db.dropDatabase()//销毁数据库db.repairDatabase()db.shutdownServer()//关闭服务MongoDBshell管理数据库第四部分MongoDB高级应用2324MongoVUE-一个windows下的客户端管理工具MongoHUB-Mac下的MongoDB客户端。ServerDensity-是一个商业的监控服务提供商rock_mongo-PHP写的一个web工具MongoDB图形化管理工具25MongoDB导出集合MongoDB提供了mongoexport工具,可以把一个collection导出成json格式或csv格式的文件。可以指定导出哪些数据项,也可以根据给定的条件导出数据。-h[--host]-u[--username]argusername-p[--password]argpassword-d[--db]argdatabasetouse-c[--collection]argcollectiontouse(somecommands)-f[