您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 1mongodb初步使用总结
mongoDB2.6使用总结一、准备工作下载java驱动包驱动包下载地址:下载:在线api:二、安装Mongo1.windows下安装方式:安装Mongo数据库:第一步:下载安装包:如果是win系统,注意是64位还是32位版本的,请选择正确的版本(偶数为发布版、奇数为开发版)。第二步:新建目录“F:\mongodb2”,解压下载到的安装包第三步:在“F:\mongodb2”目录下新建“db”文件夹,它将会作为数据存放的根文件夹。配置Mongo服务端:打开CMD窗口,按照如下方式输入命令:d:cdF:\mongodb2\binmongod--dbpath../db启动成功后从浏览器访问:标明windows下的mongodb已经启动成功;数据库启动完成,接下来是创建数据库和集合:mongoshowdbsadmin(empty)local0.078GBuseadmin(切换管理用户)switchedtodbadmindb.my_mongo(创建数据库)admin.my_mongodb.addUser(abc,abc123)(添加用户)WARNING:The'addUser'shellhelperisDEPRECATED.Pleaseuse'createUser'insteadSuccessfullyaddeduser:{user:abc,roles:[root]}db.auth(abc,abc123)(添加登陆用户)1db.createCollection(t_users)(添加表){ok:1}showcollections(显示表)system.indexessystem.userssystem.versiont_usersdb.t_users.save({age:21})(存储数据)WriteResult({nInserted:1})db.t_users.find()(查询所有数据){_id:ObjectId(53a2e45e4a674863b4ac5398),age:21}mongodb删除数据库usemymongo;db.dropDatabase();mongodb删除表db.t_users.drop();三、Java操作MongoDB示例1、建立Test.java,完成简单的mongoDB数据库操作packagecom;importjava.net.UnknownHostException;importjava.util.Date;importcom.mongodb.BasicDBObject;importcom.mongodb.DB;importcom.mongodb.DBCollection;importcom.mongodb.DBCursor;importcom.mongodb.DBObject;importcom.mongodb.Mongo;importcom.mongodb.MongoException;publicclassTest{staticDBCollectioncoll=null;staticDBdb=null;static{Mongom=null;try{m=newMongo(localhost,27017);}catch(UnknownHostExceptione){e.printStackTrace();}catch(MongoExceptione){e.printStackTrace();}db=m.getDB(my_mongo);if(db.authenticate(prx,prx123456.toCharArray())){System.out.println(authsuccess);}}publicstaticvoidmain(String[]args)throwsException{//getMemo();//insertMemo();query();//delete();}/****@paramcollectionName相当于Table名*@return*@throwsException*/publicstaticDBCollectiongetDBCollection(StringcollectionName)throwsException{if(coll==null){coll=db.getCollection(collectionName);}returncoll;}publicstaticvoidinsertMemo()throwsException{DBCollectioncoll=getDBCollection(member);BasicDBObjectdoc=newBasicDBObject();doc.put(name,prx);doc.put(city,changsha);doc.put(time,newDate());coll.insert(doc);}publicstaticvoidgetMemo()throwsException{DBCollectioncoll=getDBCollection(member);BasicDBObjectobj=(BasicDBObject)coll.findOne();System.out.println(obj);}publicstaticvoidquery()throwsException{DBCollectioncoll=getDBCollection(member);BasicDBObjectobj=newBasicDBObject();obj.put(name,prx);DBCursorcursor=coll.find(obj);while(cursor.hasNext()){Datedate=(Date)cursor.next().get(time);System.out.println(date);}cursor.close();}publicstaticvoiddelete()throwsException{DBCollectioncoll=getDBCollection(member);BasicDBObjectquery=newBasicDBObject();query.put(name,prx);//找到并且删除,并返回删除的对象DBObjectremoveObj=coll.findAndRemove(query);System.out.println(removeObj);}}四、mongoDb语法与mysql语法比较MongoDB语法MySql语法db.test.find({'name':'foobar'})==select*fromtestwherename='foobar'db.test.find()==select*fromtestdb.test.find({'ID':10}).count()==selectcount(*)fromtestwhereID=10db.test.find().skip(10).limit(20)==select*fromtestlimit10,20db.test.find({'ID':{$in:[25,35,45]}})==select*fromtestwhereIDin(25,35,45)db.test.find().sort({'ID':-1})==select*fromtestorderbyIDdescdb.test.distinct('name',{'ID':{$lt:20}})==selectdistinct(name)fromtestwhereID20db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})==selectname,sum(marks)fromtestgroupbynamedb.test.find('this.ID20',{name:1})==selectnamefromtestwhereID20db.test.insert({'name':'foobar','age':25})==insertintotest('name','age')values('foobar',25)db.test.remove({})==delete*fromtestdb.test.remove({'age':20})==deletetestwhereage=20db.test.remove({'age':{$lt:20}})==eletetestwhereage20db.test.remove({'age':{$lte:20}})==deletetestwhereage=20db.test.remove({'age':{$gt:20}})==deletetestwhereage20db.test.remove({'age':{$gte:20}})==deletetestwhereage=20db.test.remove({'age':{$ne:20}})==deletetestwhereage!=20db.test.update({'name':'foobar'},{$set:{'age':36}})==updatetestsetage=36wherename='foobar'db.test.update({'name':'foobar'},{$inc:{'age':3}})==updatetestsetage=age+3wherename='foobar'五、主从复制主服务器上的数据能同步到从服务器。六、副本集)--清空所有db文件夹2)--primarymongod--dbpath=D:\mongodb2\db--port2222--replSetshopex/127.0.0.1:33333)--secondarymongod--dbpath=E:\mongodb2\db--port3333--replSetshopex/127.0.0.1:22224)--初始化db.runCommand({replSetInitiate:{_id:shopex,members:[{_id:1,host:127.0.0.1:2222},{_id:2,host:127.0.0.1:3333}]}})5)--仲裁mongod--dbpath=F:\mongodb2\db--port4444--replSetshopex/127.0.0.1:22226)--客户端2222/adminmongo127.0.0.1:2222/adminrs.addArb(127.0.0.1:4444)rs.status()7)---故障自动回复KO掉2222稍等一会儿,3333自动变成primary8)--客户端3333/adminrs.status()9)---恢复启动2222,2222变成secondary,3333是primary,4444仍是仲裁。七、分片技术
本文标题:1mongodb初步使用总结
链接地址:https://www.777doc.com/doc-3024724 .html