/** * Countable: count * * Count the results from the current query: pass FALSE for "all" results (disregard limit/skip)<br/> * Count results of a separate query: pass an array or JSON string of query parameters * * @param mixed $query * @throws Exception * @return int */ public function count($query = TRUE) { if (is_bool($query)) { // Profile count operation for cursor if ($this->db()->profiling) { $bm = $this->db()->profiler_start("Mongo_Database::{$this->db}", $this->inspect() . ".count(" . JSON::str($query) . ")"); } $this->_cursor or $this->load(TRUE); $count = $this->_cursor->count($query); } else { if (is_string($query) && $query[0] == "{") { $query = JSON::arr($query); if ($query === NULL) { throw new Exception('Unable to parse query from JSON string.'); } } $query_trans = array(); foreach ($query as $field => $value) { $query_trans[$this->get_field_name($field)] = $value; } $query = $query_trans; // Profile count operation for collection if ($this->db()->profiling) { $bm = $this->db()->profiler_start("Mongo_Database::{$this->db}", "db.{$this->name}.count(" . ($query ? JSON::str($query) : '') . ")"); } $count = $this->collection()->count($query); } // End profiling count if (isset($bm)) { $this->db()->profiler_stop($bm); } if (is_array($count)) { throw new MongoException(json_encode($count)); } return $count; }
/** * Load the document from the database. The first parameter may be one of: * * a falsey value - the object data will be used to construct the query * a JSON string - will be parsed and used for the query * an non-array value - the query will be assumed to be for an _id of this value * an array - the array will be used for the query * * @param array specify additional criteria * @param array specify the fields to return * @return boolean TRUE if the load succeeded */ public function load($criteria = array(), array $fields = array()) { // Use of json for querying is allowed if (is_string($criteria) && $criteria[0] == "{") { $criteria = JSON::arr($criteria); } else { if ($criteria && !is_array($criteria)) { $criteria = array('_id' => $criteria); } else { if (isset($this->_object['_id'])) { $criteria = array('_id' => $this->_object['_id']); } else { if (isset($criteria['id'])) { $criteria = array('_id' => $criteria['id']); } else { if (!$criteria) { $criteria = $this->_object; } } } } } if (!$criteria) { throw new MongoException('Cannot find ' . get_class($this) . ' without _id or other search criteria.'); } // Cast query values to the appropriate types and translate aliases $new = array(); foreach ($criteria as $key => $value) { $key = $this->get_field_name($key); $new[$key] = $this->_cast($key, $value); } $criteria = $new; // Translate field aliases $fields = array_map(array($this, 'get_field_name'), $fields); $values = $this->collection()->__call('findOne', array($criteria, $fields)); // Only clear the object if necessary if ($this->_loaded !== NULL || $this->_changed || $this->_operations) { $this->clear(); } $this->load_values($values, TRUE); return $this->_loaded; }