Collection.php 4.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 15/6/13
 * Time: 下午4:50
 */

namespace Hood\Dao\MongoDB;

class Collection extends Cursor
{

    /**
     * Enter description here...
     *
     * @var MongoCore
     */
    private $_db;

    /**
     * @var string
     */
    private $_dbName;

    /**
     * 返回的结果集
     * @var array
     */
    private $_fields = array();

    /**
     * @var string
     */
    private $_collectionName;

    private $_set = false;

    public function __construct(\MongoClient $mongo, $dbName, $collection)
    {
        $this->_dbName = $dbName;
        $this->_collectionName = $collection;
        $this->_db = $mongo->selectDB($this->_dbName);
        $this->_collection = $mongo->selectCollection($this->_dbName, $this->_collectionName);
    }

    /**
     * 插入数据
     * @param $attrs
     * @param array $options
     * @return MongoResult
     */
    public function insert($attrs, array $options = array())
    {
        $result = new MongoResult();
        $result->status = $bool = $this->_collection->insert($attrs, $options);
        $result->attrs = $attrs;
        if ($bool) {
            $result->_id = $attrs["_id"];
        }
        return $result;
    }

    /**
     * 查询所有数据
     * @param array $query
     * @param array $fields
     * @return array
     */
    public function find(array $query = array(), array $fields = array())
    {
        return $this->_collection->find($query, $fields);
    }

    /**
     * 查询所有符合条件的数据
     * @return \MongoCursor
     */
    public function findAll()
    {
        return $this->_cursor();
    }

    /**
     * 查找一行数据
     * @param array $query
     * @param array $fields
     * @return array|null
     */
    public function findOne(array $query = array(), array $fields = array())
    {
        $_fields = empty($fields) ? $this->_fields : $fields;
        return $this->_collection->findOne($query, $_fields);
    }

    /**
     * 设置返回字段
     * @param array $fields
     * @return $this
     */
    public function fields(array $fields)
    {
        $this->_fields = $fields;
        return $this;
    }

    /**
     * 更新
     * @param array $criteria
     * @param array $newobj
     * @param array $options
     * @return bool
     */
    public function update(array $criteria, array $newobj, array $options = array())
    {
        if ($this->_set && !isset($newobj['$set'])) {
            $newobj = array('$set' => $newobj);
        }
        return $this->_collection->update($criteria, $newobj, $options);
    }

    /**
     * 从集合中删除记录
     * @param array $criteria
     * @param array $options
     * @return array|bool
     */
    public function remove(array $criteria = array(), array $options = array())
    {
        return $this->_collection->remove($criteria, $options);
    }


    public function set()
    {
        $this->_set = true;
        return $this;
    }

    /**
     * 查找一行数据,但只返回ID数据
     * @param $id
     * @return array|null
     */
    public function findID($id, array $fields = array())
    {
        $_fields = empty($fields) ? $this->_fields : $fields;
        if (($id instanceof \MongoId) == false) {
            $id = new \MongoId($id);
        }
        return $this->_collection->findOne(array('_id' => $id), $_fields);
    }

    /**
     * 保存一个文档到集合
     * 如果对象来自数据库,则更新现有的数据库对象,否则插入对象
     * @param $a
     * @param array $options
     * @return array|bool
     */
    public function save($a, array $options = array())
    {
        return $this->_collection->save($a, $options);
    }

    /**
     * 查询并更新数据
     * @param array $query
     * @param array $update
     * @param array $fields
     * @param array $options
     * @return array
     */
    public function findAndModify(array $query, array $update = NULL, array $fields = NULL, array $options = NULL)
    {
        return $this->_collection->findAndModify($query, $update, $fields, $options);
    }

    /**
     * 获取总数
     * @param array $query
     * @return int
     */
    public function count($query = array())
    {
        return $this->_collection->count($query);
    }

    /**
     * 批量插入一组新的数据
     * @param array $a
     * @param array $options
     * @return mixed
     */
    public function batchInsert(array $a, array $options = array())
    {
        return $this->_collection->batchInsert($a, $options);
    }
}