MongoDB::command

(PECL mongo >=0.9.2)

MongoDB::commandִ��һ�� Mongo ָ��

˵��

public MongoDB::command ( array $command [, array $options = array() ] ) : array

�������в�����CRUD���������鶼����ͨ��һ��"���ݿ�ָ��"��ɡ���Ҫ֪�����ݿ�İ汾����һ��ָ�����ʵ�֡���Ҫ����һ�ξۺϣ���һ��ָ�����ʵ�֡���Ҫ�����־������һ��ָ�����ʵ�֡��������Ѿ���ᵽ�ˡ�

�÷�����ͬ�ڣ�

<?php

public function command($data) {
    return 
$this->selectCollection('$cmd')->findOne($data);
}

?>

����

command

Ҫ���͵�ָ��

options

�ò�����һ������������ʽ�Ĺ������飺 array("optionname" => <boolean>, ...)������֧�ֵ�ѡ���У�

  • "timeout"

    Integer, defaults to MongoCursor::$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.

������־

�汾 ˵��
1.2.0 ��� options ��������һ��ѡ��:"timeout"��

����ֵ

�������ݿ���Ӧ��ÿ����Ӧ�����ᳬ��һ���ĵ��Ĵ�С��Ҳ���Dz��ᳬ��16MB�� ����ĵ��Ľṹ��ִ�е�ָ���й�,���󲿷ֽ������ ok �ֶ�����ʾ�ɹ�����ʧ�ܡ��Լ� results �ֶΰ���һ������ĵ����顣

����

Example #1 MongoDB::command() "distinct"ʵ��

����һ���������в��ظ�ֵ

<?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";
}

?>

�������̵���������ڣ�


4
22
87

Example #2 MongoDB::command() "distinct"ʵ��

����һ���������в��ظ�ֵ��������Щֵ���ڵ���18

<?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";
}

?>

�������̵���������ڣ�


22
87

Example #3 MongoDB::command() MapReduceʵ��

��ȡ������type=sale������"event"���û����Լ����Ƿֱ��м���������"event"��ע:�˴���event��һ�����ϵ����֣�

<?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";
}

?>

�������̵���������ڣ�


User 47cc67093475061e3d9536d2 had 3 sale(s).
User 49902cde5162504500b45c2c had 14 sale(s).
User 4af467e4fd543cce7b0ea8e2 had 1 sale(s).

Note: ʹ�� MongoCode

�������ʹ���� MongoCode���������Խ���һ�������������Ȼ�������� MongoDB ����֧���� MapReduce ��ʹ������ �������Ҫ��MapReduce��������һ���ͻ��˲�������ô�������ʹ��MapReduce��ʱ����"optional scope"�ֶΰ�������ӵ�ȫ���������У��ο� » MapReduce�ĵ� ����ø�����Ϣ��

Note: out ����

1.8.0��ǰ��out �����ǿ�ѡ�ģ�����㲻ʹ������MapReduce�Ľ������д��һ����ʱ����������ʱ���ϻ������ӹرպ�ɾ���� 1.8.0�Ժ�out �����DZ���ģ��ο� » MapReduce documentation ����ø�����Ϣ��

Example #4 MongoDB::command() "textSearch"ʵ��

��MongoDB 2.4���ϰ汾��ʹ��ȫ�ļ������ܣ�֮ǰ�İ汾��֧��ȫ�ļ�������

<?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);
?>

�������̵���������ڣ�


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
)

Example #5 MongoDB::command() "geoNear"ʵ��

���ʵ��˵�������ʹ�� geoNear ָ�

<?php
$m 
= new MongoClient();
$d $m->demo;
$c $d->poiConcat;

$r $d->command(array(
    
'geoNear' => "poiConcat",      // �� poiConcat ������
    
'near' => array(-0.0851.48), // ���� 51.48��N, 0.08��E ����
    
'spherical' => true,           // ������������
    
'num' => 5,                    // ��෵��5���ĵ�
));
print_r($r);
?>

�μ�

MongoDB �����ĵ��� » ���ݿ�ָ�� ���Լ���Щ�ض�ָ����ĵ� » findAndModify�� » getLastError�� » repairDatabase �����кܶ�����ָ���ֻ��һЩ���ӣ�