mongo.init.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.1 国产PHP开发框架 Dao-Nosql-Mongo
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * $Author:zhuli
  10. * $Dtime:2013-5-29
  11. ***********************************************************************************/
  12. class mongoInit {
  13. private $mongo; //mongo对象
  14. private $db; //db mongodb对象数据库
  15. private $collection; //集合,相当于数据表
  16. /**
  17. * 初始化Mongo
  18. * $config = array(
  19. * 'server' => ‘127.0.0.1' 服务器地址
  20. * ‘port’ => '27017' 端口地址
  21. * ‘option’ => array('connect' => true) 参数
  22. * 'db_name'=> 'test' 数据库名称
  23. * ‘username’=> 'zhuli' 数据库用户名
  24. * ‘password’=> '123456' 数据库密码
  25. * )
  26. * Enter description here ...
  27. * @param unknown_type $config
  28. */
  29. public function init($config = array()) {
  30. if ($config['server'] == '') $config['server'] = '127.0.0.1';
  31. if ($config['port'] == '') $config['port'] = '27017';
  32. if (!$config['option']) $config['option'] = array('connect' => true);
  33. $server = 'mongodb://' . $config['server'] . ':' . $config['port'];
  34. $this->mongo = new Mongo($server, $config['option']);
  35. if ($config['db_name'] == '') $config['db_name'] = 'test';
  36. $this->db = $this->mongo->selectDB($config['db_name']);
  37. if ($config['username'] != '' && $config['password'] != '')
  38. $this->db->authenticate($config['username'], $config['password']);
  39. }
  40. /**
  41. * 选择一个集合,相当于选择一个数据表
  42. * @param string $collection 集合名称
  43. */
  44. public function selectCollection($collection) {
  45. return $this->collection = $this->db->selectCollection($collection);
  46. }
  47. /**
  48. * 新增数据
  49. * @param array $data 需要新增的数据 例如:array('title' => '1000', 'username' => 'xcxx')
  50. * @param array $option 参数
  51. */
  52. public function insert($data, $option = array()) {
  53. return $this->collection->insert($data, $option);
  54. }
  55. /**
  56. * 批量新增数据
  57. * @param array $data 需要新增的数据 例如:array(0=>array('title' => '1000', 'username' => 'xcxx'))
  58. * @param array $option 参数
  59. */
  60. public function batchInsert($data, $option = array()) {
  61. return $this->collection->batchInsert($data, $option);
  62. }
  63. /**
  64. * 保存数据,如果已经存在在库中,则更新,不存在,则新增
  65. * @param array $data 需要新增的数据 例如:array(0=>array('title' => '1000', 'username' => 'xcxx'))
  66. * @param array $option 参数
  67. */
  68. public function save($data, $option = array()) {
  69. return $this->collection->save($data, $option);
  70. }
  71. /**
  72. * 根据条件移除
  73. * @param array $query 条件 例如:array(('title' => '1000'))
  74. * @param array $option 参数
  75. */
  76. public function remove($query, $option = array()) {
  77. return $this->collection->remove($query, $option);
  78. }
  79. /**
  80. * 根据条件更新数据
  81. * @param array $query 条件 例如:array(('title' => '1000'))
  82. * @param array $data 需要更新的数据 例如:array(0=>array('title' => '1000', 'username' => 'xcxx'))
  83. * @param array $option 参数
  84. */
  85. public function update($query, $data, $option = array()) {
  86. return $this->collection->update($query, $data, $option);
  87. }
  88. /**
  89. * 根据条件查找一条数据
  90. * @param array $query 条件 例如:array(('title' => '1000'))
  91. * @param array $fields 参数
  92. */
  93. public function findOne($query, $fields = array()) {
  94. return $this->collection->findOne($query, $fields);
  95. }
  96. /**
  97. * 根据条件查找多条数据
  98. * @param array $query 查询条件
  99. * @param array $sort 排序条件 array('age' => -1, 'username' => 1)
  100. * @param int $limit 页面
  101. * @param int $limit 查询到的数据条数
  102. * @param array $fields返回的字段
  103. */
  104. public function find($query, $sort = array(), $skip = 0, $limit = 0, $fields = array()) {
  105. $cursor = $this->collection->find($query, $fields);
  106. if ($sort) $cursor->sort($sort);
  107. if ($skip) $cursor->skip($skip);
  108. if ($limit) $cursor->limit($limit);
  109. return iterator_to_array($cursor);
  110. }
  111. /**
  112. * 数据统计
  113. */
  114. public function count() {
  115. return $this->collection->count();
  116. }
  117. /**
  118. * 错误信息
  119. */
  120. public function error() {
  121. return $this->db->lastError();
  122. }
  123. /**
  124. * 获取集合对象
  125. */
  126. public function getCollection() {
  127. return $this->collection;
  128. }
  129. /**
  130. * 获取DB对象
  131. */
  132. public function getDb() {
  133. return $this->db;
  134. }
  135. }