0
我使用蒙戈殼牌此查詢的值來獲得陣列「事件」的元素,以「啓動」場PHP的MongoDB的聚集與匹配和排序順序
db.collection_name.aggregate(
{ $match: {
_id : ObjectId("59941bec47582c1e92b93c9b")
}},
{ $unwind: '$events' },
{ $sort: {
'events.start': 1
}})
但我不瞭解如何在php中做同樣的事情。 我試圖寫這個沒有結果:
$client = new MongoClient("mongodb://admin:[email protected]");
$collection = $client->db_name->collection_name;
$cursor = $collection->aggregate([
['$match' => ['_id' =>new MongoDB\BSON\ObjectID("59941bec47582c1e92b93c9b")]],
['$unwind' => '$events'],
['$sort' => ['events.start' => 1]]
]);
foreach($cursor as $document) {
var_dump($document);
}
我在這等方式也試過:
$manager = new MongoDB\Driver\Manager("mongodb://admin:[email protected]:27017");
$command = new MongoDB\Driver\Command([
'aggregate' => 'collection_name',
'pipeline' => [
['$match' => ['_id' =>new MongoDB\BSON\ObjectID("59941bec47582c1e92b93c9b")]],
['$unwind' => '$events'],
['$sort' => ['events.start' => 1]]
],
]);
$cursor = $manager->executeCommand('db_name', $command);
foreach($cursor as $key=>$document) {
var_dump($document);
}
你的查詢看起來不錯。請考慮添加您期望查詢的示例文檔以返回。通過運行簡單查找查詢,驗證您是否通過正確的權限連接到正確的mongo server&db。 – Veeram
是的,謝謝。第二個代碼有一個身份驗證問題。我現在已經在連接url中添加了db名稱。 – aleknx1000