I am new to MongoDB aggregation.
My DB has the following structure:
{
"_id": "AAABBBCCCDDDD",
"report":{
"google": [{
"report_id": "XXX",
"detail": [{
"available": true,
"status_code": 200,
"return_code": "ok",
}, {
"available": true,
"status_code": 300,
"return_code": "ok",
}, {
"available": true,
"status_code": 400,
"return_code": "ok",
}]
}, {
"report_id": "YYY",
"detail": [{
"available": false,
"status_code": 200,
"return_code": "ok",
}, {
"available": true,
"status_code": 200,
"return_code": "ng",
}, {
"available": true,
"status_code": 200,
"return_code": "ok",
}]
}]
}
}
I want to flatten the documents like this:
{
"AAABBBCCCDDDD": [{
"report_id": "XXX",
"detail": [{
"available": true,
"status_code": 200,
"return_code": "ok",
}, {
"available": true,
"status_code": 300,
"return_code": "ok",
}, {
"available": true,
"status_code": 400,
"return_code": "ok",
}]
}, {
"report_id": "YYY",
"detail": [{
"available": false,
"status_code": 200,
"return_code": "ok",
}, {
"available": true,
"status_code": 200,
"return_code": "ng",
}, {
"available": true,
"status_code": 200,
"return_code": "ok",
}]
}]
}
then count how many matched available is true and return_code is "ok", returning a structure like this:
{
"AAABBBCCCDDDD": [{
"report_id": "XXX",
"available_count": 3,
},
{
"report_id": "YYY",
"available_count": 1,
}]
}
Is there anyway to do this?
truein available.