如何使用php 7 mongo驱动程序管理器进行聚合和$ lookup。
如何将以下mongo命令转换为php
db.a.aggregate([{$lookup:{from:"b",localField:"business_id",foreignField:"_id",as:"contact"}}])
同时推荐好的参考教程。
答案 0 :(得分:2)
此代码有效
$mng = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$command = new MongoDB\Driver\Command([
'aggregate' => 'a',
'pipeline' => [
['$lookup' => ["from" => "b","localField" => "business_id","foreignField" => "_id","as" => "contact"]],
],
]);
$cursor = $mng->executeCommand('test', $command);
答案 1 :(得分:1)
尝试这样的事情:
$mongo->db->a->aggregate([
['$lookup' => ['from' => 'b', 'localField' => 'business_id', 'foreignField' => '_id', 'as' => 'contact'] ]
]);
答案 2 :(得分:0)
PHP7.2
MongoDB 4.2
我从@吉宾·马修(Jibin Mathew)的答案中获得了参考,但是我遇到了一些错误。
然后我添加了
'光标'=>新的stdClass,
这行代码。
现在它可以正常工作了。
//mongo database join example
public function test_join() {
global $mng;
global $dbname;
$pipeline = [['$lookup' => ["from" => "b","localField" => "business_id","foreignField" => "_id","as" => "contact"]]];
$aggregate = new \MongoDB\Driver\Command([
'aggregate' => 'a',
'cursor' => new stdClass,
'pipeline' => $pipeline,
]);
$cursor = $mng->executeCommand($dbname, $aggregate);
return $cursor;
}