I have a documents like that at my mongo database:
{
{"_id" : NumberLong(50),
"offerId" : NumberLong(88),
"isInTryPeriod" : false,
"state" : {
"currentState" : "INACTIVE",
"oldState" : "INACTIVE"
},
......
......
......
}
And I want to group them under "offerId" but also i want to see the count isInTryPeriod (for true and false) and currentState (active, inactive etc) number under specific states like as follows:
{{
_id: offerId
states: {
isInTryperiodCountForThatOfferId: X
activeForThatOfferId: X
inactiveForThatOfferId: X
},
{
_id: offerId
states: {
isInTryperiodCountForThatOfferId: X
activeForThatOfferId: X
inactiveForThatOfferId: X
}}
What i tried for that is, I write two query for them they work separately but i want to compose them.
To get count for sate.currentState field
db.subscriptions.aggregate(
{
$group: {
_id: { offerId: "$offerId", state: "$state.currentState" },
"state": { "$push": "$state.currentState" },
"total": { "$sum": 1 }
}
},
{
$group: {
_id: { offerId: "$_id.offerId" },
state: { $addToSet: { state: "$_id.state", sum:"$total" } }
}
}
);
To get the count for isInTrpPreiod field
db.subscriptions.aggregate([
{
$match : {isInTryPeriod : true}
},
{
$group: {
_id: { offerId: "$offerId", "isInTryPeriod" : "$isInTryPeriod"},
"isInTryPeriod": { "$push": "$isInTryPeriod" },
"total": { "$sum": 1 }
}
},
{
$group: {
_id: "$_id.offerId",
isInTryPeriod: { $addToSet: { isInTryPeriod: "$_id.isInTryPeriod", sum:"$total" } }
}
}
]);