问题
I have 3 to 4 collections
- users
- payments
- branches
- grades
In users table we have multiple types of users like student, parents like this.
In payments table user payments details have payments details with partical amount or full amount.
In branches, table have branch details.
I am trying find the reports based on branches and grades wise.
- total numbers of users in that branch
- total numbers of paid users in that branch based on paidStatus column.
- total sum amount recieved in that branch based on receivedAmount column(Sum)
- total balance group by user and branchId
- branch name.
Sample user table:
[
{
"_id": "5b372ea5b0103b4ab2409ea6",
"name" : "User 1",
"role" : "student",
"enrollmentCode": "X1",
"branchId": "5b36388c7e50f13e845346e2",
"classId" : "5b36388c7e50f13e84534343"
},
{
"_id": "5b372ea5b0103b4ab2409ea7",
"name" : "User 2",
"role" : "student",
"enrollmentCode": "X2",
"branchId": "5b36388c7e50f13e845346e2",
"classId" : "5b36388c7e50f13e8453433d"
},
{
"_id": "5b372ea5b0103b4ab2409ea9",
"name" : "User 3",
"role" : "student",
"enrollmentCode": "X3",
"branchId": "5b36388c7e50f13e845346e2",
"classId" : "5b36388c7e50f13e8453433x"
}
]
Payments table:
[
{
"_id": "5b372ea5b0103b4ab2409e32",
"userId": "5b372ea5b0103b4ab2409ea6",
"enrollmentCode": "X1",
"branchId": "5b36388c7e50f13e845346e2",
"classId" : "5b36388c7e50f13e8453433d",
"receivedAmount": 5000,
"balanceAmount": 2000,
"paidStatus": false
},
{
"_id": "5b372ea5b0103b4ab2409e32",
"userId": "5b372ea5b0103b4ab2409ea6",
"enrollmentCode": "X1",
"branchId": "5b36388c7e50f13e845346e2",
"classId" : "5b36388c7e50f13e8453433d",
"receivedAmount": 1000,
"balanceAmount": 500,
"paidStatus": false
},
{
"_id": "5b372ea5b0103b4ab2409e32",
"userId": "5b372ea5b0103b4ab2409ea7",
"classId" : "5b36388c7e50f13e8453433d",
"enrollmentCode": "X2",
"branchId": "5b36388c7e50f13e845346e2",
"receivedAmount": 1000,
"balanceAmount": 500,
"paidStatus": true
}
]
Branche table
[
{
"_id": "5b36388c7e50f13e845346e2",
"name": "Test 1"
},
{
"_id": "5b36388c7e50f13e845346e2",
"name": "Test 2"
},
{
"_id": "5b36388c7e50f13e845346e2",
"name": "Test 3"
}
]
Class table same us branches.
My Query:
db.users.aggregate([ { $match : { role : "student"}} , { $group : { _id : "$student.branchId", totalStudents : { $sum : 1 }}}, {$lookup: {from: "branches", localField: "_id", foreignField: "_id", as: "branches"}}, {$project : { _id : 1, totalStudents : 1, 'branches.branchCode' : 1, 'branches.name' :1}}] )
I can able get the students count, branch name and all but i have no idea to group by payments table and sum the amount..
My query output:
[{ "_id" : ObjectId("5b89161d1326f1646fabb59e"), "totalStudents" : 295, "receivedAmount" : 1000, "branches" : [ {
"name" : "abcd" } ] }
{ "_id" : ObjectId("5b47343d55b8344687674b7b"), "totalStudents" : 104, "branches" : [ { "name" : "xxxx" } ] }]
Expected output:
[
{
"_id": ObjectId("5b89161d1326f1646fabb59e"),
"totalStudents": 295,
"receivedAmount" : 12345,
"paidStudents" : 1,
"branches": [
{
"name": "abcd"
}
]
}
{
"_id": ObjectId("5b47343d55b8344687674b7b"),
"totalStudents": 104,
"receivedAmount" : 12345,
"paidStudents" : 1,
"branches": [
{
"name": "xxxx"
}
]
}
]
来源:https://stackoverflow.com/questions/60867408/mongodb-group-by-with-multiple-collections