Sorry for the title I don't know how to express the problem better.
I'm trying to sum the total spent money on book items in the MongoDB document. That means before calculating the array must be filtered that only bought books are considered when calculating the total spent money.
This is an example document (I removed some fields so it doesn't get too big):
{
"_id" : ObjectId("598cb3f4ca693a0688004e2a"),
"title" : {
"de" : "Another",
"ja" : "アナザー",
"roomaji" : "Another"
},
"total" : 4,
"volumes" : [
{
"volume" : 1,
"edition" : 1,
"price" : 7,
"releaseDate" : 1342051200,
"bought" : true,
"read" : true
},
{
"volume" : 2,
"edition" : 1,
"price" : 7,
"releaseDate" : 1347494400,
"bought" : true,
"read" : true
},
{
"volume" : 3,
"edition" : 1,
"price" : 7,
"releaseDate" : 1352419200,
"bought" : false,
"read" : false
},
{
"volume" : 4,
"edition" : 1,
"price" : 7,
"releaseDate" : 1357776000,
"bought" : false,
"read" : false
}
],
"releaseYear" : 2012
}
So, I want to sum the price field of all volume objects, but only those where "bought" is set to true. As result I would like to get this output:
{
"_id" : ObjectId(),
"title" : {
'de' : 'title'
},
"count" : 14
}
And this is my PHP-Code
$result = $mongo->Book->aggregate([
[
'$group' => [
'_id' => '$_id',
'title' => ['$push' => '$title.de'],
'count' => [
'$sum' => [
'$map' => [
'input' => '$volumes',
'as' => 'vol',
'in' => [
'$cond' => [
[
'$eq' => ['$$vol.bought', true]
],
'$$vol.price',
0
]
]
],
]
]
]
],
[
'$sort' => [
'count' => -1
]
]
]);
But count always returns 0. What I'm doing wrong?