我在mongoDB和php的第一步,试图找出聚合如何工作。我有一个关于如何从命令行使用它们的大概想法,但我正在尝试为php驱动程序翻译它。我现在用的是餐馆dexample DB,记录列表像这样PHP MongoDB驱动程序和聚合
{
"_id" : ObjectId("59a5211e107765480896f3f8"),
"address" : {
"building" : "284",
"coord" : [
-73.9829239,
40.6580753
],
"street" : "Prospect Park West",
"zipcode" : "11215"
},
"borough" : "Brooklyn",
"cuisine" : "American",
"grades" : [
{
"date" : ISODate("2014-11-19T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2013-11-14T00:00:00Z"),
"grade" : "A",
"score" : 2
},
{
"date" : ISODate("2012-12-05T00:00:00Z"),
"grade" : "A",
"score" : 13
},
{
"date" : ISODate("2012-05-17T00:00:00Z"),
"grade" : "A",
"score" : 11
}
],
"name" : "The Movable Feast",
"restaurant_id" : "40361606"
}
我只是想有多少餐馆位置算,我在做什么是
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->myNewDb->restaurants;
$results = $collection->aggregate(['name' => '$name'],['$group' => ['cuisine' => ['sum' => '$sum']]]);
和我得到这个错误
Fatal error: Uncaught exception 'MongoDB\Exception\InvalidArgumentException' with message '$pipeline is not a list (unexpected index: "name")'
任何想法?我无法在php.net上找到任何好的文档。
感谢 中号
你是什么意思“翻译”?几乎每种语言都有很简单的事情可以与广泛使用的JavaScript Object Notation样本交换。基本前提是每个'{}'或者甚至''''都被PHP替换为'[]'或'array()'。所以考虑到这一点,你甚至不会接近语法。 '$ group'需要'_id'。我不知道你认为'['name'=>'$ name']'应该做什么。也许你甚至应该说明你的预期输出应该是什么。 –