1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 332247 $ -->
<refentry xml:id="mongodb.command" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoDB::command</refname>
<refpurpose>Execute a database command</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>array</type><methodname>MongoDB::command</methodname>
<methodparam><type>array</type><parameter>command</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter><initializer>array()</initializer></methodparam>
</methodsynopsis>
<para>
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.
</para>
<para>
This method is identical to:
<programlisting role="php">
<![CDATA[
<?php
public function command($data) {
return $this->selectCollection('$cmd')->findOne($data);
}
?>
]]>
</programlisting>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>command</parameter>
</term>
<listitem>
<para>
The query to send.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>options</parameter>
</term>
<listitem>
<para>
This parameter is an associative array of the form
<literal>array("optionname" => <boolean>, ...)</literal>. Currently
supported options are:
<itemizedlist>
&mongo.writes.parameters.timeout;
</itemizedlist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>1.2.0</entry>
<entry>
Added <literal>options</literal> parameter with a single option:
<literal>"timeout"</literal>.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns database response. Every database response is always maximum one
document, which means that the result of a database command can never
exceed 16MB. The resulting document's structure depends on the command, but
most results will have the <literal>ok</literal> field to indicate success
or failure and <literal>results</literal> containing an array of each of
the resulting documents.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoDB::command</function> "distinct" example</title>
<para>
Finding all of the distinct values for a key.
</para>
<programlisting role="php">
<![CDATA[
<?php
$people = $db->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";
}
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
4
22
87
</screen>
</example>
<example>
<title><function>MongoDB::command</function> "distinct" example</title>
<para>
Finding all of the distinct values for a key, where the value is larger
than or equal to 18.
</para>
<programlisting role="php">
<![CDATA[
<?php
$people = $db->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",
"query" => array("age" => array('$gte' => 18))
)
);
foreach ($ages['values'] as $age) {
echo "$age\n";
}
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
22
87
</screen>
</example>
<example>
<title><function>MongoDB::command</function> MapReduce example</title>
<para>
Get all users with at least on "sale" event, and how many times each
of these users has had a sale.
</para>
<programlisting role="php">
<![CDATA[
<?php
// sample event document
$events->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";
}
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
User 47cc67093475061e3d9536d2 had 3 sale(s).
User 49902cde5162504500b45c2c had 14 sale(s).
User 4af467e4fd543cce7b0ea8e2 had 1 sale(s).
</screen>
<note>
<title>Using <classname>MongoCode</classname></title>
<para>
This example uses <classname>MongoCode</classname>, 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
<link xlink:href="&url.mongodb.dochub.mapreduce;">MapReduce documentation</link>
for more information.
</para>
</note>
<note>
<title>The <literal>out</literal> argument</title>
<para>
Before 1.8.0, the <literal>out</literal> 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
<literal>out</literal> argument is required. See the
<link xlink:href="&url.mongodb.dochub.mapreduce;">MapReduce documentation</link>
for more information.
</para>
</note>
<para>
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
<link xlink:href="&url.mongodb.mapreduceapi;">Github</link>
and there is a
<link xlink:href="&url.mongodb.mapreduceapi.blog;">blog post</link>
on how to use it.
</para>
</example>
<example>
<title><function>MongoDB::command</function> "textSearch" example</title>
<para>
Do a fulltext search lookup with MongoDB's 2.4 and higher "text search"
functionality.
</para>
<programlisting role="php">
<![CDATA[
<?php
$m = new MongoClient();
$d = $m->demo;
$c = $d->planets;
$c->insert(array("name" => "Mercury", "desc" => "Mercury is the smallest and closest to the Sun"));
$c->insert(array("name" => "Venus", "desc" => "Venus is the second planet from the Sun, orbiting it every 224.7 Earth days."));
$c->insert(array("name" => "Earth", "desc" => "Earth is the the densest of the eight planets in the Solar System."));
$c->insert(array("name" => "Mars", "desc" => "Mars is named after the Roman god of war."));
$c->ensureIndex(array('desc' => 'text'));
$r = $d->command(array("text" => "planets", 'search' => "sun" ));
print_r($r);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
Array
(
[queryDebugString] => sun||||||
[language] => english
[results] => Array
(
[0] => Array
(
[score] => 0.625
[obj] => Array
(
[_id] => MongoId Object
(
[$id] => 517549d944670a4a5cb3059a
)
[name] => Mercury
[desc] => Mercury is the smallest and closest to the Sun
)
)
[1] => Array
(
[score] => 0.55
[obj] => Array
(
[_id] => MongoId Object
(
[$id] => 517549d944670a4a5cb3059b
)
[name] => Venus
[desc] => Venus is the second planet from the Sun, orbiting it every 224.7 Earth days.
)
)
)
[stats] => Array
(
[nscanned] => 2
[nscannedObjects] => 0
[n] => 2
[nfound] => 2
[timeMicros] => 95
)
[ok] => 1
)
</screen>
</example>
<example>
<title><function>MongoDB::command</function> "geoNear" example</title>
<para>
This example shows how to use the geoNear command.
</para>
<programlisting role="php">
<![CDATA[
<?php
$m = new MongoClient();
$d = $m->demo;
$c = $d->poiConcat;
$r = $d->command(array(
'geoNear' => "poiConcat", // Search in the poiConcat collection
'near' => array(-0.08, 51.48), // Search near 51.48°N, 0.08°E
'spherical' => true, // Enable spherical search
'num' => 5, // Maximum 5 returned documents
));
print_r($r);
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><methodname>MongoCollection::aggregate</methodname></member>
<member><methodname>MongoCollection::findAndModify</methodname></member>
<member><methodname>MongoCollection::group</methodname></member>
</simplelist>
</para>
<para>
MongoDB core docs on
<link xlink:href="&url.mongodb.dochub.commands;">database commands</link>
and on individual commands:
<link xlink:href="&url.mongodb.dochub.findandmodify;">findAndModify</link>,
<link xlink:href="&url.mongodb.dochub.getlasterror;">getLastError</link>, and
<link xlink:href="&url.mongodb.dochub.repairdatabase;">repairDatabase</link> (dozens more
exist, there are merely a few examples).
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|