MongoDB::command Execute a database command &reftitle.description; public arrayMongoDB::command arraycommand arrayoptionsarray() Almost everything that is not a CRUD operation can be done with a database command. Need to know the database version? There's a command for that. Need to do aggregation? There's a command for that. Need to turn up logging? You get the idea. This method is identical to: selectCollection('$cmd')->findOne($data); } ?> ]]> &reftitle.parameters; command The query to send. options This parameter is an associative array of the form array("optionname" => <boolean>, ...). Currently supported options are: "timeout" Integer, defaults to Mongo::$timeout. If "safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, a MongoCursorTimeoutException will be thrown. &reftitle.changelog; &Version; &Description; 1.2.0 Added options parameter with a single option: timeout. &reftitle.returnvalues; Returns database response. &reftitle.examples; <function>MongoDB::command</function> "distinct" example Finding all of the distinct values for a key. people; $people->insert(array("name" => "Joe", "age" => 4)); $people->insert(array("name" => "Sally", "age" => 22)); $people->insert(array("name" => "Dave", "age" => 22)); $people->insert(array("name" => "Molly", "age" => 87)); $ages = $db->command(array("distinct" => "people", "key" => "age")); foreach ($ages['values'] as $age) { echo "$age\n"; } ?> ]]> &example.outputs.similar; 4 22 87 <function>MongoDB::command</function> MapReduce example Get all users with at least on "sale" event, and how many times each of these users has had a sale. insert(array("user_id" => $id, "type" => $type, "time" => new MongoDate(), "desc" => $description)); // construct map and reduce functions $map = new MongoCode("function() { emit(this.user_id,1); }"); $reduce = new MongoCode("function(k, vals) { ". "var sum = 0;". "for (var i in vals) {". "sum += vals[i];". "}". "return sum; }"); $sales = $db->command(array( "mapreduce" => "events", "map" => $map, "reduce" => $reduce, "query" => array("type" => "sale"), "out" => array("merge" => "eventCounts"))); $users = $db->selectCollection($sales['result'])->find(); foreach ($users as $user) { echo "{$user['_id']} had {$user['value']} sale(s).\n"; } ?> ]]> &example.outputs.similar; User 47cc67093475061e3d9536d2 had 3 sale(s). User 49902cde5162504500b45c2c had 14 sale(s). User 4af467e4fd543cce7b0ea8e2 had 1 sale(s). Using <classname>MongoCode</classname> This example uses MongoCode, which can also take a scope argument. However, at the moment, MongoDB does not support using scopes in MapReduce. If you would like to use client-side variables in the MapReduce functions, you can add them to the global scope by using the optional scope field with the database command. See the MapReduce documentation for more information. The <literal>out</literal> argument Before 1.8.0, the out argument was optional. If you did not use it, MapReduce results would be written to a temporary collection, which would be deleted when your connection was closed. In 1.8.0+, the out argument is required. See the MapReduce documentation for more information. If you are going to be using MapReduce, Prajwal Tuladhar created an API for Mongo PHP users which provides a nicer interface than the bare command. You can download it from Github and there is a blog post on how to use it. &reftitle.seealso; MongoDB core docs on database commands and on individual commands: findAndModify, getLastError, and repair (dozens more exist, there are merely a few examples).