查看: 3292|回复: 0

mongoDB操作实例

[复制链接]
论坛徽章:
0
跳转到指定楼层
1#
发表于 2012-2-29 16:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
先看一个简单的例子:
  • <?php  
  • $m = new Mongo();      //这里采用默认连接本机的27017端口,当然你也可以连接远程主机如                   192.168.0.4:27017,如果端口是27017,端口可以省略
  •   
  • $db = $m -> comedy;             // 选择comedy数据库,如果以前没该数据库会自动创建,也可以用$m->selectDB("comedy");  
  •   
  • $collection = $db->collection;  //选择comedy里面的collection集合,相当于RDBMS里面的表,也-可以使用  
  • $db->selectCollection("collection");  
  •   
  • $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );  
  •   
  • $collection->insert($obj);     //将$obj 添加到$collection 集合中  
  •   
  •   
  • $obj = array( "title" => "XKCD", "online" => true );  
  • $collection->insert($obj);  
  • $cursor = $collection->find();  
  •   
  • foreach ($cursor as $obj) {    //遍历所有集合中的文档  
  • echo $obj["title"] . "\n";  
  • }      
  •   
  •   
  • $m->close();          //断开MongoDB连接  


下面在介绍一些常用的函数:
  • $query = array( "i" => 71 );  
  • $cursor = $collection->find( $query );      // 在$collectio集合中查找满足$query的文档  
  •   
  • while( $cursor->hasNext() ) {  
  • var_dump( $cursor->getNext() );  
  • }      
  •   
  • $collection -> findOne();            //返回$collection集合中第一个文档  
  •   
  • $collection -> count();              //返回$collection集合中文档的数量  
  • $coll->ensureIndex( array( "i" => 1 ) );  // 为i “这一列”加索引 降序排列  
  • $coll->ensureIndex( array( "i" => -1, "j" => 1 ) );  // 为i “这一列”加索引 降序排列 j升序  


查询时,每个Object插入时都会自动生成一个独特的_id,它相当于RDBMS中的主键,用于查询时非常方便
如:
  • <?php  
  •   
  • $person = array("name" => "joe");  
  •   
  • $people->insert($person);  
  •   
  • $joe = $people->findOne(array("_id" => $person['_id']));  
  •   
  • ?>  

更新时:

假如我们想修改下面文档中comments中author的名字
  • {   
  •     "_id" : ObjectId("4b06c282edb87a281e09dad9"),   
  •     "content" : "this is a blog post.",  
  •     "comments" :   
  •     [  
  •         {  
  •             "author" : "Mike",  
  •             "comment" : "I think that blah blah blah...",  
  •         },  
  •         {  
  •             "author" : "John",  
  •             "comment" : "I disagree."  
  •         }  
  •     ]  
  • }  


为了改变内部的一个域, 我们用$set (保证文档中其他域不被移除)并且comment的索引也变化
Php代码
  • <?php  
  •   
  • $collection->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim")))); //$criteria 为要更新的元素  
  •   
  • ?>  

删除一个数据库
Php代码
  • $m -> dropDB("comedy");  


列出所有可用数据库
Php代码
  • $m->listDBs();   //无返回值  
         
    好了就先 写这么多了,有兴趣的话可以在网上搜到其他的关于Mongo-php     API的用法!
命令行使用实例:
  • db.system.users.find()  
  • db.users.count()  
  • db.users.ensureIndex({password:-1})  
  • use test  
  • db.users.getIndexes()  
  • db.repairDatabase()   
  • show users  
  • show dbs  
  • db.users.find({username:{$in:['4d81a82398790']}}).explain()  
  • db.users.dropIndexes()   
  • db.users.find().count()  
  • db.users.find().limit(5)  
  • db.users.find({"username":"ssa"})   
  • show collections   
  • db.users.remove()  
  • db.user.remove({'username':'admin'})  
  • db.user.insert({'username':'admin','age':21,'nickname':'admin'})  
  • db.user.save({'username':'admin','age':21,'info':['12','12313','zzsd']})  
  • db.createCollection("user")   
  • db.dropDatabase()   
  • show collections   
  • db.test.drop()   
  • db.copyDatabase('test','test1')  
  • show profile  
  • db.printCollectionStats()  
  • db.addUser('admin','admin123')   
  • db.setProfilingLevel(2);  
  • db.setProfilingLevel( 1 , 10 );  
  • db.system.profile.find()  

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表