我正在使用jenssegers与Laravel合作开发mongoDB。我试图总结基于Abonos.price。 我有一个像这样的MongoDb文档:
`{
"_id" : ObjectId("594a89358b85112444002924"),
"cliente" : "blabla",
"tipo" : "bla bla",
"paquete" : "bla bla",
"date" : "2017-06-14",
"content" : "picture",
"cost" : NumberInt(200),
"status" : NumberInt(2),
"Abonos" : [
{
"price" : "200", `enter code here`
"date" : "2017-06-21"
},
{
"price" : "300",
"date" : null
}
], }`
我想总结Abonos.price以获得" 500" (200 + 300) 我在laravel控制器中有这个功能:
$result = Work::raw(function($collection){
return $collection->aggregate(array(
array('$unwind' => '$Abonos'),
array('$group' => array(
"_id" => '$_id',
"total" => array('$sum' => '$Abonos.price')
)),
));});
return $result;
但是我得到了
[{"_id":"594a95cc8b85112444002925","total":0}
而不是
[{"_id":"594a95cc8b85112444002925","total":500}"
我想$return = 500