2009-05-20 22:31:40 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
|
2009-07-11 08:06:30 +00:00
|
|
|
<!-- $Revision$ -->
|
2009-05-29 20:03:49 +00:00
|
|
|
<refentry xml:id="mongodb.command" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
2009-05-20 22:31:40 +00:00
|
|
|
<refnamediv>
|
2009-05-29 20:03:49 +00:00
|
|
|
<refname>MongoDB::command</refname>
|
|
|
|
<refpurpose>Execute a database command</refpurpose>
|
2009-05-20 22:31:40 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
|
|
|
<methodsynopsis>
|
2009-05-29 20:03:49 +00:00
|
|
|
<modifier>public</modifier> <type>array</type><methodname>MongoDB::command</methodname>
|
2009-05-20 22:31:40 +00:00
|
|
|
<methodparam><type>array</type><parameter>data</parameter></methodparam>
|
|
|
|
</methodsynopsis>
|
2009-11-06 23:57:31 +00:00
|
|
|
<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>
|
2009-05-20 22:31:40 +00:00
|
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<parameter>data</parameter>
|
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The query to send.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
|
|
|
Returns database response.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
2009-11-06 23:57:31 +00:00
|
|
|
<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->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> map/reduce 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,
|
2009-11-20 20:51:16 +00:00
|
|
|
"query" => array("type" => "sale")));
|
2009-11-06 23:57:31 +00:00
|
|
|
|
|
|
|
$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>
|
|
|
|
</example>
|
|
|
|
</refsect1>
|
2009-05-20 22:31:40 +00:00
|
|
|
</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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2009-05-20 22:31:40 +00:00
|
|
|
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
|
|
|
|
-->
|