【发布时间】:2020-11-02 13:23:55
【问题描述】:
我正在尝试让用户在 laravel 中使用 mongodb 发布帖子,此查询在 mongodb shell 中运行良好,但在 laravel 中无法正常运行,并且出现此错误:
一个流水线阶段规范对象必须只包含一个字段。
这是我在 mongodb shell 中的查询:
db.instaPosts.aggregate([
{"$match" :{ "userid" : "1507073550"}},
{"$group" :
{ "_id" : "$post.location.id",
"location" : {"$first":"$post.location.name"},
"count":{ "$sum" : 1}}}
])
结果如下:
{ "_id" : null, "location" : null, "count" : 7 }
{ "_id" : "332558707", "location" : "Daryache Chitgar,Tehran,Iran", "count" : 1 }
{ "_id" : "250445386", "location" : "دانشگاه علم و فرهنگ", "count" : 1 }
{ "_id" : "1343757649052341", "location" : "Agor Caffe", "count" : 1 }
{ "_id" : "111106416225578", "location" : "Santiago Bernabeu, Madrid", "count" : 1 }
{ "_id" : "506047058", "location" : "Cardiff City Stadium", "count" : 1 }
这是我的控制器:
$places = Posts::raw(function ($collection){
return $collection->aggregate([
[
'$match' => [
'userid' => [ '$eq' => '1507073550']
],
'$group'=> [
'_id' => '$post.location.id',
'location' => [
'$first'=>'$post.location.name'
],
'total' => [
'$sum' => 1
],
]
]
]);
});
完全相同的东西!但它不起作用。
【问题讨论】: