当前位置:首页 > 商业/管理/HR > 经营企划 > MongoDB 学习笔记
MongoDB学习笔记[mongo]////////////////////MongoDB命令行帮助helpdb.help()数据库帮助db.mycoll.help()集合帮助rs.help()helponreplicasetmethodshelpconnect链接服务器帮助helpadmin管理命令helpmisc杂项showdbsshowdatabasenamesshowcollectionsshowcollectionsincurrentdatabaseshowusersshowusersincurrentdatabaseshowprofileshowmostrecentsystem.profileentrieswiusedb_name选择数据库db.foo.find()查询集合db.foo.find({a:1})条件查询集合itresultofthelastlineevaluated;usetofurtheriterateexitquitthemongoshell////////////////////////////手册////教程?php//链接数据库$connection=newMongo();//默认端口27017$connection=newMongo();$connection=newMongo();$m=newMongo(mongodb://${username}:${password}@localhost:12345/blog,array('persist'='p'));//验证用户名密码$db-authenticate($username,$password);//获取数据库$db=$connection-blog;//获取集合$collection=$db-users;$collection=$connection-blog-users;$doc=array('username'='admin','password'='admin');//添加$collection-insert($doc);//获取第一个$obj=$collection-findOne();//获取集合的数量echo$collection-count();//获得所有文档使用游标$cursor=$collection-find();//返回的是MongoCursor对象foreach($cursoras$id=$value){echo$id:;var_dump($value);}//条件查询$query=array(i=71);$cursor=$collection-find($query);while($cursor-hasNext()){var_dump($cursor-getNext());}//条件查询$query=array(i=array('$gt'=50));//$gt,$lt,$gte=,$lte=$query=array(i=array(\$gt=20,\$lte=30));//多条件$cursor=$coll-find($query);while($cursor-hasNext()){var_dump($cursor-getNext());}//设置查询的特殊字符mongo.cmd=://创建索引$coll-ensureIndex(array(i=1));//i正序$coll-ensureIndex(array(i=-1,j=1));//i正序,j倒序//关闭连接$connection-close();//负载均衡$m=newMongo(mongodb://localhost:27017,array(replicaSet=true));$result=$m-admin-command(array(ismaster=1));//持久连接$m=newMongo(localhost:27017,array(persist=x));//域套接字$m=newMongo(mongodb://username:password@/tmp/mongo-27017.sock:0/foo);////写//安全操作$collection-insert($someDoc);$collection-update($criteria,$newObj);$collection-insert($somethingElse);$collection-remove($something,array(safe=true));$collection-insert($someDoc,array(safe=3));////查询Query//根据id查询MongoId$person=array(name=joe);$people-insert($person);$joe=$people-findOne(array(_id=$person['_id']));$person=array(name=joe);$people-insert($person);//转换_id为一个字符串$pid=$person['_id'].;//FAILS(失败)-$pid是一个字符串,不是一个MongoId对象$joe=$people-findOne(array(_id=$pid));//索引的数组$collection-save(array(awards=array(gold,silver,bronze)));//{_id:ObjectId(4b06c282edb87a281e09dad9),awards:[gold,silver,bronze]}$cursor=$collection-find(array(awards=gold));//键值的数组或对象{_id:ObjectId(4b06c282edb87a281e09dad9),awards:[{firstplace:gold},{secondplace:silver},{thirdplace:bronze}]}$cursor=$collection-find(array(awards.firstplace=gold));//或者$cursor=$collection-find(array(awards=array('$in'=array(gold,copper))));////更新update//更新字段$coll-update(array(username=joe),array('$set'=array(twitter=@joe4153)));//更新文档$coll-update(array(username=joe),array(userId=12345,info=array(name=joe,twitter=@joe4153,email=...),likes=array()));//更新嵌套的对象{_id:ObjectId(4b06c282edb87a281e09dad9),content:thisisablogpost.,comments:[{author:Mike,comment:Ithinkthatblahblahblah...,},{author:John,comment:Idisagree.}]}//更新制定下标$blog-update($criteria,array('$set'=array(comments.1=array(author=Jim))));//更显所有的下标$代表当前的下标$blog-update(array(comments.author=John),array('$set'=array('comments.$.author'=Jim)));////安全//防止注入一般情况下不要拼接字符串//////////////////////////////////核心类Mongo//数据库连接MongoDBMongoCollectionMongoCursor{/*Constants常量*/conststringMongo::VERSION;//驱动程序版本conststringMongo::DEFAULT_HOST=localhost;constintMongo::DEFAULT_PORT=27017;/*Fields字段*/publicboolean$connected=FALSE;publicstring$status=NULL;protectedstring$server=NULL;protectedboolean$persistent=NULL;/*Methods方法*/publicbooleanclose(void)//关闭数据库连接publicbooleanconnect(void)//连接到数据库服务器protectedbooleanconnectUtil(void)__construct([string$server=mongodb://localhost:27017[,array$options=array(connect=TRUE)]])//persist持久连接timeout链接超时publicarraydropDB(mixed$db)//已废弃publicMongoDB__get(string$dbname)//获取数据库publicarraylistDBs(void)//获取所有数据库列表publicMongoCollectionselectCollection(string$db,string$collection)//获取数据库集合publicMongoDBselectDB(string$name)//获取数据库publicstring__toString(void)//本连接字符串表示形式}MongoDB{/*Constants*/constintMongoDB::PROFILING_OFF=0;constintMongoDB::PROFILING_SLOW=1;constintMongoDB::PROFILING_ON=2;/*Fields*/publicinteger$w=1;publicinteger$wtimeout=10000;///*Methods*/publicarrayauthenticate(string$username,string$password)//登录publicarraycommand(array$data)//数据库命令__construct(Mongo$conn,string$name)publicMongoCollectioncreateCollection(string$name[,bool$capped=FALSE[,int$size=0[,int$max=0]]])publicarraycreateDBRef(string$collection,mixed$a)//创建一个外键publicarraydrop(void)//删除此数据库publicarraydropCollection(mixed$coll)publicarrayexecute(mixed$code[,array$args=array()])//执行数据库服务器上的JavaScript代码publicboolforceError(void)publicMongoCollection__get(string$name)//获取集合publicarraygetDBRef(array$ref)//获取一个外键publicMongoGridFSgetGridFS([string$prefix=fs])publi
本文标题:MongoDB 学习笔记
链接地址:https://www.777doc.com/doc-4917875 .html