(PECL mongo >=0.9.2)
MongoDB::command — 执锟斤拷一锟斤拷 Mongo 指锟斤拷
$command
[, array $options
= array()
] ) : array锟斤拷锟斤拷锟斤拷锟叫诧拷锟斤拷锟斤拷CRUD锟斤拷锟斤拷锟斤拷锟斤拷锟介都锟斤拷锟斤拷通锟斤拷一锟斤拷"锟斤拷锟捷匡拷指锟斤拷"锟斤拷伞锟斤拷锟揭拷锟斤拷锟斤拷菘锟侥版本锟斤拷锟斤拷一锟斤拷指锟斤拷锟斤拷锟绞碉拷帧锟斤拷锟揭拷锟斤拷锟揭伙拷尉酆希锟斤拷锟揭伙拷锟街革拷锟斤拷锟斤拷实锟街★拷锟斤拷要锟斤拷锟斤拷锟街撅拷锟斤拷锟斤拷锟揭伙拷锟街革拷锟斤拷锟斤拷实锟街★拷锟斤拷锟斤拷锟斤拷锟窖撅拷锟斤拷岬斤拷恕锟�
锟矫凤拷锟斤拷锟斤拷同锟节o拷
<?php
public function command($data) {
return $this->selectCollection('$cmd')->findOne($data);
}
?>
command
要锟斤拷锟酵碉拷指锟斤拷
options
锟矫诧拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷式锟侥癸拷锟斤拷锟斤拷锟介: array("optionname" => <boolean>, ...)锟斤拷锟斤拷锟斤拷支锟街碉拷选锟斤拷锟叫o拷
"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"锟斤拷 |
锟斤拷锟斤拷锟斤拷锟捷匡拷锟斤拷应锟斤拷每锟斤拷锟斤拷应锟斤拷锟斤拷锟结超锟斤拷一锟斤拷锟侥碉拷锟侥达拷小锟斤拷也锟斤拷锟角诧拷锟结超锟斤拷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";
}
?>
锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�
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";
}
?>
锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�
Example #3 MongoDB::command() MapReduce实锟斤拷
锟斤拷取锟斤拷锟斤拷锟斤拷type=sale锟斤拷锟斤拷锟斤拷"event"锟斤拷锟矫伙拷锟斤拷锟皆硷拷锟斤拷锟角分憋拷锟叫硷拷锟斤拷锟斤拷锟斤拷锟斤拷"event"锟斤拷注:锟剿达拷锟斤拷event锟斤拷一锟斤拷锟斤拷锟较碉拷锟斤拷锟街o拷
<?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";
}
?>
锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�
Note: 使锟斤拷 MongoCode
锟斤拷锟斤拷锟斤拷锟绞癸拷锟斤拷锟� MongoCode锟斤拷锟斤拷锟斤拷锟斤拷锟皆斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟饺伙拷锟斤拷锟斤拷锟斤拷锟� MongoDB 锟斤拷锟斤拷支锟斤拷锟斤拷 MapReduce 锟斤拷使锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟揭拷锟組apReduce锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟酵伙拷锟剿诧拷锟斤拷锟斤拷锟斤拷么锟斤拷锟斤拷锟斤拷锟绞癸拷锟組apReduce锟斤拷时锟斤拷锟斤拷"optional scope"锟街段帮拷锟斤拷锟斤拷锟斤拷拥锟饺拷锟斤拷锟斤拷锟斤拷锟斤拷校锟斤拷慰锟� » MapReduce锟侥碉拷 锟斤拷锟斤拷酶锟斤拷锟斤拷锟较拷锟�
Note: out 锟斤拷锟斤拷
1.8.0锟斤拷前锟斤拷out 锟斤拷锟斤拷锟角匡拷选锟侥o拷锟斤拷锟斤拷悴皇癸拷锟斤拷锟斤拷锟組apReduce锟侥斤拷锟斤拷锟斤拷锟叫达拷锟揭伙拷锟斤拷锟绞憋拷锟斤拷锟斤拷铮拷锟斤拷锟斤拷时锟斤拷锟较伙拷锟斤拷锟斤拷锟接关闭猴拷删锟斤拷锟斤拷 1.8.0锟皆猴拷out 锟斤拷锟斤拷锟角憋拷锟斤拷模锟斤拷慰锟� » MapReduce documentation 锟斤拷锟斤拷酶锟斤拷锟斤拷锟较拷锟�
Example #4 MongoDB::command() "textSearch"实锟斤拷
锟斤拷MongoDB 2.4锟斤拷锟较版本锟斤拷使锟斤拷全锟侥硷拷锟斤拷锟斤拷锟杰o拷之前锟侥版本锟斤拷支锟斤拷全锟侥硷拷锟斤拷锟斤拷锟斤拷
<?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);
?>
锟斤拷锟斤拷锟斤拷锟教碉拷锟斤拷锟斤拷锟斤拷锟斤拷冢锟�
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.08, 51.48), // 锟斤拷锟斤拷 51.48锟斤拷N, 0.08锟斤拷E 锟斤拷锟斤拷
'spherical' => true, // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
'num' => 5, // 锟斤拷喾碉拷锟�5锟斤拷锟侥碉拷
));
print_r($r);
?>
MongoDB 锟斤拷锟斤拷锟侥碉拷锟斤拷 » 锟斤拷锟捷匡拷指锟斤拷 锟斤拷锟皆硷拷锟斤拷些锟截讹拷指锟斤拷锟斤拷牡锟� » findAndModify锟斤拷 » getLastError锟斤拷 » repairDatabase 锟斤拷锟斤拷锟叫很讹拷锟斤拷锟斤拷指锟筋,锟斤拷只锟斤拷一些锟斤拷锟接o拷