I have two distinct documents as
Accounts
[
{_id:1, name:'xyz'},
{_id:2, name:'abc'},
{_id:3, name:'def'},
{_id:4, name:'pqr'},
]
and
Responses
[
{_id:01, accountId:2, questionId: 0001, res: true},
{_id:02, accountId:2, questionId: 0002, res: true},
{_id:03, accountId:1, questionId: 0003, res: false},
{_id:03, accountId:3, questionId: 0002, res: false},
{_id:03, accountId:2, questionId: 0003, res: false},
]
How can I count number of true and false responses for an individual account while maintaining its original fields too.
Eg.
{
_id: 2,
name: 'abc',
trueResponses: 2,
falseResponses: 1
}
I have tried using $lookup to join the other document in a field but am unable to count different responses.
db.accounts.aggregate([
{
$match: { _id: 2 }
},
{
$lookup:{
from: 'responses',
localField: '_id',
foreignField: 'accountId',
as: 'responses'
}
}
])