免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2599 | 回复: 0
打印 上一主题 下一主题

[MongoDB] mongodb test practice [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-01 11:33 |只看该作者 |倒序浏览
mongodb test practice



这几天接触了NOSQL数据库mongodb,做了一些测试练习,在此整理一下。

Php代码

  1. /**
  2. * mongodb {name:'mongo', type=>'db'}
  3. * Test practice
  4. *
  5. * @author flyer0126
  6. * @date 2011/08/31
  7. */

  8. // connect (connect the unit 27017 port default).
  9. $mongo = new Mongo();

  10. //Lists all of the databases available.
  11. $dbs = $mongo->listDBs();

  12. //Gets a database named 'flyer'.
  13. $flyerdb = $mongo->selectDB('flyer');

  14. //Get a list of collections in 'flyerdb' database.
  15. $flyers = $flyerdb->listCollections();

  16. //Gets a collection named 'flyer0126'.
  17. $flyer0126 = $flyerdb->selectCollection('flyer0126');

  18. //record array.
  19. $obj = array('title'=>'测试标题', 'author'=>'测试作者', 'created'=>date('Y-m-d H:i:s'));

  20. //Saves $obj1 to this collection.
  21. $flyer0126->save($obj);

  22. //Counts the number of documents where {title:'测试标题1'} in this collection.
  23. $number = $flyer0126->count(array('title'=>'测试标题'));

  24. //Querys this collection $flyer0126.
  25. $res = $flyer0126->find();
  26. foreach ($res as $obj){
  27.         print_r($obj);
  28.         echo "<br/>";
  29. }

  30. //echo content:
  31. /*Array ( [_id] => MongoId Object ( [$id] => 4e5d979d250813880c000016 ) [title] => 测试标题 [author] => 测试作者 [created] => 2011-08-31 10:08:29 )*/

  32. //Querys this collection, returning a single element(Similar to 'limit 1' in sql).
  33. $res1 = $flyer0126->findOne();
  34. print_r($res1);
  35. echo "<br/>";

  36. //echo content:
  37. /*Array ( [_id] => MongoId Object ( [$id] => 4e5d979d250813880c000016 ) [title] => 测试标题 [author] => 测试作者 [created] => 2011-08-31 10:08:29 )*/

  38. //Update records based on a given criteria.
  39. $flyer0126->update(array('title'=>'测试标题'), array('$set' => array('title'=>'测试标题test')));

  40. //Querys this collection 'flyer0126'.
  41. $res = $flyer0126->find();
  42. foreach ($res as $obj){
  43.         print_r($obj);
  44.         echo "<br/>";
  45. }

  46. //echo content:
  47. /*Array ( [_id] => MongoId Object ( [$id] => 4e5d979d250813880c000016 ) [author] => 测试作者 [created] => 2011-08-31 10:08:29 [title] => 测试标题test )*/

  48. //Remove records from this collection.
  49. $flyer0126->remove(array('title'=>'测试标题'));

  50. //Drops this collection 'flyer0126'.
  51. $flyer0126->drop('flyer0126');

  52. //Drops a database 'flyer'.
  53. $mongo->dropDB('flyer');
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP