mongodb + group by with multiple collections

一世执手 提交于 2020-04-17 22:05:17

问题


I have 3 to 4 collections

  1. users
  2. payments
  3. branches
  4. 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.

  1. total numbers of users in that branch
  2. total numbers of paid users in that branch based on paidStatus column.
  3. total sum amount recieved in that branch based on receivedAmount column(Sum)
  4. total balance group by user and branchId
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!