/** * Communicates with the database to log in a user. * * @param MongoDB $db database * @param string $username username * @param string $pwd plaintext password * * @return array the database response */ protected static function getUser($db, $username, $pwd) { $ns = "{$db}.system.users"; // get the nonce $result = $db->command(array(MongoUtil::NONCE => 1)); if (!$result["ok"]) { return $result; } $nonce = $result["nonce"]; // create a digest of nonce/username/pwd $digest = md5($nonce . $username . $pwd); $data = array(MongoUtil::AUTHENTICATE => 1, "user" => $username, "nonce" => $nonce, "key" => $digest); // send everything to the db and pray return $db->command($data); }
/** * Return an array of stored tags * * @return array array of stored tags (string) */ public function getTags() { $cmd['mapreduce'] = $this->_options['collection']; $cmd['map'] = 'function(){ this.t.forEach( function(z){ emit( z , { count : 1 } ); } ); };'; $cmd['reduce'] = 'function( key , values ){ var total = 0; for ( var i=0; i<values.length; i++ ) total += values[i].count; return { count : total }; };'; $cmd['out'] = array('replace' => 'getTagsCollection'); $res2 = $this->_db->command($cmd); $res3 = $this->_db->selectCollection('getTagsCollection')->find(); $res = array(); foreach ($res3 as $key => $val) { $res[] = $key; } $this->_db->dropCollection($res2['result']); return $res; }
/** * command. */ public function command($command, array $options = array(), &$hash = NULL) { $this->time->start(); $return = parent::command($command, $options); $time = $this->time->stop(); $this->log(array('type' => 'command', 'options' => $options, 'time' => $time)); return $return; }
/** * command. */ public function command($data) { $this->time->start(); $return = parent::command($data); $time = $this->time->stop(); $this->log(array('type' => 'command', 'data' => $data, 'time' => $time)); return $return; }
/** * Wrapper method for MongoDB::command(). * * @see http://php.net/manual/en/mongodb.command.php * @param array $command Command document * @param array $options Client-side options (e.g. socket timeout) * @param string $hash Optional reference argument to collect the server * hash for command cursors (for driver 1.5+ only) * @return array */ public function command(array $command, array $options = [], &$hash = null) { $options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options; if (func_num_args() > 2) { return $this->mongoDB->command($command, $options, $hash); } return $this->mongoDB->command($command, $options); }
/** * Command. * * Runs a MongoDB command (such as GeoNear). See the MongoDB documentation * for more usage scenarios - http://dochub.mongodb.org/core/commands * * @param array $query The command query * * @access public * @return \MongoQB\Builder */ public function command($query = array()) { try { $execute = $this->_dbhandle->command($query); return $execute; } catch (\MongoCursorException $Exception) { throw new \MongoQB\Exception('MongoDB command failed to execute: ' . $Exception->getMessage()); // @codeCoverageIgnoreEnd } }
/** * Distinct * Select distinct from collection * @param array $query The search query * @param string $distinct The collection name * @access public * @return \MongoQB\Builder */ public function distinct($distinct = "", $key = "", $query = array()) { try { $query = array('distinct' => $distinct, 'key' => $key, 'query' => $query); $execute = $this->_dbhandle->command($query); return isset($execute['values']) ? $execute['values'] : false; } catch (\MongoCursorException $Exception) { throw new Exception('MongoDB command failed to execute: ' . $Exception->getMessage()); // @codeCoverageIgnoreEnd } }
public function testDBCommand() { $x = $this->object->command(array()); $this->assertEquals(0, strpos($x['errmsg'], "no such cmd"), json_encode($x)); $this->assertEquals((bool) $x['ok'], false); $created = $this->object->createCollection("system.profile", true, 5000); $this->object->command(array('profile' => 0)); $x = $this->object->command(array('profile' => 1)); $this->assertEquals($x['was'], 0, json_encode($x)); $this->assertEquals((bool) $x['ok'], true, json_encode($x)); }
/** * Performs an operation similar to SQL's GROUP BY command * * @param mixed $keys - Fields to group by. If an array or non-code * object is passed, it will be the key used to group results. * @param array $initial - Initial value of the aggregation counter * object. * @param mongocode $reduce - A function that takes two arguments (the * current document and the aggregation to this point) and does the * aggregation. * @param array $options - Optional parameters to the group command * * @return array - Returns an array containing the result. */ public function group($keys, array $initial, $reduce, array $options = array()) { $cmd = ['group' => ['ns' => $this->name, 'key' => $keys, '$reduce' => $reduce, 'initial' => $initial]]; if (isset($options['finalize'])) { $cmd['group']['finalize'] = $options['finalize']; } if (isset($options['condition'])) { $cmd['group']['cond'] = $options['condition']; } return $this->db->command($cmd); }
/** * @param string $table * @return array */ public function getTableStats($table) { $stats = array(); if (!$this->collectionExists($table)) { return $stats; } $rawstats = $this->dbcon->command(array('collStats' => $table)); $stats['rows'] = $rawstats['count']; if ($table = TeraWurflConfig::$TABLE_PREFIX . 'Merge') { $collection = $this->dbcon->selectCollection($table); $tofind = array('actual_device_root' => 1); $res = $collection->find($tofind); $stats['actual_devices'] = $res->count(); } $stats['bytesize'] = $rawstats['storageSize']; return $stats; }
/** * @link http://www.php.net/manual/en/mongocollection.aggregate.php * @param array $pipeline * @param array $op * @return array */ public function aggregate(array $pipeline, array $op = []) { if (!TypeConverter::isNumericArray($pipeline)) { $pipeline = []; $options = []; $i = 0; foreach (func_get_args() as $operator) { $i++; if (!is_array($operator)) { trigger_error("Argument {$i} is not an array", E_WARNING); return; } $pipeline[] = $operator; } } else { $options = $op; } $command = ['aggregate' => $this->name, 'pipeline' => $pipeline]; $command += $options; return $this->db->command($command, [], $hash); }
/** * Performs an operation similar to SQL's GROUP BY command * * @link http://www.php.net/manual/en/mongocollection.group.php * @param mixed $keys Fields to group by. If an array or non-code object is passed, it will be the key used to group results. * @param array $initial Initial value of the aggregation counter object. * @param MongoCode|string $reduce A function that aggregates (reduces) the objects iterated. * @param array $condition An condition that must be true for a row to be considered. * @return array */ public function group($keys, array $initial, $reduce, array $condition = []) { if (is_string($reduce)) { $reduce = new MongoCode($reduce); } $command = ['group' => ['ns' => $this->name, '$reduce' => (string) $reduce, 'initial' => $initial, 'cond' => $condition]]; if ($keys instanceof MongoCode) { $command['group']['$keyf'] = (string) $keys; } else { $command['group']['key'] = $keys; } if (array_key_exists('condition', $condition)) { $command['group']['cond'] = $condition['condition']; } if (array_key_exists('finalize', $condition)) { if ($condition['finalize'] instanceof MongoCode) { $condition['finalize'] = (string) $condition['finalize']; } $command['group']['finalize'] = $condition['finalize']; } return $this->db->command($command); }
public function returnsDistinct(ObjectModel\Collection $collection, ObjectModel\Property\PropertyAbstract $property) { $class = $collection->getDataObject()->getClass(); $mode = $collection->getParameter('memberType'); // set database to use $this->_selectDatabase(); // get collection to use, from mapper if available, else from data object $collec = $this->_mapper instanceof Backend\Mapper ? $this->_mapper->getDatastore($class) : $class; //$collec = $this->_db->selectCollection($collec); $conditions = array(); /* @var $condition t41_Condition */ foreach ($collection->getConditions() as $conditionArray) { $condition = $conditionArray[0]; // map property to field if ($this->_mapper) { $field = $this->_mapper->propertyToDatastoreName($class, $condition->getProperty()->getId()); } else { $field = $condition->getProperty()->getId(); } $conditions += $this->_buildConditionStatement($field, $condition->getClauses(), $conditions); switch ($conditionArray[1]) { case 'OR': //$select->orWhere($statement); break; case 'AND': default: //$select->where($statement); break; } } $params = array(); $params['distinct'] = $collec; $params['key'] = $property->getId(); $params['query'] = $conditions; $this->_setLastQuery('command', $params); $ids = $this->_db->command($params); /* @todo if property is an object, we should get all values from the list of ids */ return isset($ids['values']) ? $ids['values'] : array(); }
/** * read collection information * * @param MongoDB $db database * @param string $collection collection name */ public static function info(MongoDB $db, $collection) { $ret = $db->command(array("collStats" => $collection)); if (!$ret["ok"]) { exit("There is something wrong:<font color=\"red\">{$ret['errmsg']}</font>, please refresh the page to try again."); } if (!isset($ret["retval"]["options"])) { $ret["retval"]["options"] = array(); } $isCapped = 0; $size = 0; $max = 0; $options = $ret["retval"]["options"]; if (isset($options["capped"])) { $isCapped = $options["capped"]; } if (isset($options["size"])) { $size = $options["size"]; } if (isset($options["max"])) { $max = $options["max"]; } return array("capped" => $isCapped, "size" => $size, "max" => $max); }
/** * Set MongoDb profiler * * @param int $level 0, 1, 2 * @param int $slowms slow queries in ms. * @return array */ public function setProfiler($level = self::PROFILE_ALL, $slowms = 100) { return $this->db->command(['profile' => (int) $level, 'slowms' => (int) $slowms]); }
public function testCommandTimeout() { $this->object->command(array('serverStatus' => 1), array('timeout' => -1)); }
protected function _call($command, array $arguments = array(), array $values = NULL) { $this->_connected or $this->connect(); extract($arguments); $_bm_name = isset($collection_name) ? $collection_name . '.' . $command : $command; if (isset($collection_name)) { $c = $this->_db->selectCollection($collection_name); } switch ($command) { case 'ensure_index': $r = $c->ensureIndex($keys, $options); break; case 'create_collection': $r = $this->_db->createCollection($name, $capped, $size, $max); break; case 'drop_collection': $r = $this->_db->dropCollection($name); break; case 'command': $r = $this->_db->command($values); break; case 'execute': $r = $this->_db->execute($code, $args); break; case 'batch_insert': $r = $c->batchInsert($values); break; case 'count': $r = $c->count($query); break; case 'find_one': $r = $c->findOne($query, $fields); break; case 'find': $r = $c->find($query, $fields); break; case 'group': $r = $c->group($keys, $initial, $reduce, $condition); break; case 'update': $r = $c->update($criteria, $values, $options); break; case 'insert': $r = $c->insert($values, $options); break; case 'remove': $r = $c->remove($criteria, $options); break; case 'save': $r = $c->save($values, $options); break; case 'get_file': $r = $this->gridFS()->findOne($criteria); break; case 'get_files': $r = $this->gridFS()->find($query, $fields); break; case 'set_file_bytes': $r = $this->gridFS()->storeBytes($bytes, $extra, $options); break; case 'set_file': $r = $this->gridFS()->storeFile($filename, $extra, $options); break; case 'remove_file': $r = $this->gridFS()->remove($criteria, $options); break; } if (isset($_bm)) { Profiler::stop($_bm); } return $r; }
/** * Wrapper method for MongoDB::command(). * * @see http://php.net/manual/en/mongodb.command.php * @param array $data * @param array $options * @return array */ public function command(array $data, array $options = array()) { $options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options; return $this->mongoDB->command($data, $options); }
/** collection statistics **/ public function doCollectionStats() { $this->db = x("db"); $this->collection = xn("collection"); $this->stats = array(); $db = new MongoDB($this->_mongo, $this->db); $ret = $db->command(array("collStats" => $this->collection)); if ($ret["ok"]) { $this->stats = $ret; foreach ($this->stats as $index => $stat) { if (is_array($stat)) { $this->stats[$index] = $this->_highlight($stat, "json"); } } } //top $ret = $this->_mongo->selectDB("admin")->command(array("top" => 1)); $this->top = array(); $namespace = $this->db . "." . $this->collection; if ($ret["ok"] && !empty($ret["totals"][$namespace])) { $this->top = $ret["totals"][$namespace]; foreach ($this->top as $index => $value) { $this->top[$index] = $value["count"]; } } $this->display(); }
/** * Returns the version of MongoDB, if used. * * @return array */ protected function getMongoDBVersion() { $client = new \MongoClient($this->mongoServer); $mongo = new \MongoDB($client, $this->mongoDatabase); $mongodbInfo = $mongo->command(['serverStatus' => true]); return ['mongodb_version' => $mongodbInfo['version']]; }
/** * Wrapper method for MongoDB::command(). * * @see http://php.net/manual/en/mongodb.command.php * @param array $data * @param array $options * @return array */ public function command(array $data, array $options = array()) { return $this->mongoDB->command($data, $options); }
/** * * @param MongoDB $mongoDB * @return MongoMapReduceResponse */ public function invoke(MongoDB $mongoDB, $collection) { $this->collectionNamespace = $collection; $response = $mongoDB->command($this->prepare()->_mapReduce); return new MongoMapReduceResponse($mongoDB, $response); }