Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. db.getCollection("debitors").aggregate(
  2.  
  3. // Pipeline
  4. [
  5. // Stage 1
  6. {
  7. $lookup: // Equality Match
  8. {
  9. from: "projects",
  10. localField: "fundings.project_id",
  11. foreignField: "_id",
  12. as: "projects"
  13. }
  14.  
  15. // Uncorrelated Subqueries
  16. // (supported as of MongoDB 3.6)
  17. // {
  18. // from: "<collection to join>",
  19. // let: { <var_1>: <expression>, …, <var_n>: <expression> },
  20. // pipeline: [ <pipeline to execute on the collection to join> ],
  21. // as: "<output array field>"
  22. // }
  23. },
  24.  
  25. // Stage 2
  26. {
  27. $project: {
  28. "_id":1,
  29. "name":1,
  30. "description":1,
  31. "fundings":1,
  32. "projects":{
  33. $filter: {
  34. input:"$projects",
  35. as:"pro",
  36. cond:{$not:{$eq:["$$pro.projectType", "MANAGEMENT_PROJECT"]}}
  37. }
  38. }
  39. }
  40. },
  41.  
  42. // Stage 3
  43. {
  44. $addFields: {
  45. "fundedAmount":{$sum:"$fundings.amount"}
  46. }
  47. },
  48.  
  49. // Stage 4
  50. {
  51. $group: {
  52. "_id":null,
  53. "max_funding":{$max:"$fundedAmount"},
  54. "debitors": {
  55. $addToSet:{
  56. "_id":"$_id",
  57. "name":"$name",
  58. "fundedAmount":"$fundedAmount",
  59. "fundedProjects":{$size:"$fundings"},
  60. "projects":"$projects"
  61. }
  62. }
  63. }
  64. },
  65.  
  66. // Stage 5
  67. {
  68. $project: {
  69. "debitors":{
  70. $filter:{
  71. input:"$debitors",
  72. as: "deb",
  73. cond:{$eq:["$max_funding", "$$deb.fundedAmount"]}
  74. }
  75. }
  76. }
  77. },
  78.  
  79. // Stage 6
  80. {
  81. $unwind: {
  82. path:"$debitors"
  83. }
  84. },
  85.  
  86. // Stage 7
  87. {
  88. $project: {
  89. "_id":"$debitors._id",
  90. "name":"$debitors.name",
  91. "fundedAmount":"$debitors.fundedAmount",
  92. "fundedProjects":{$size:"$debitors.projects"},
  93. "projects":"$debitors.projects.title"
  94. }
  95. },
  96.  
  97. // Stage 8
  98. {
  99. $group: {
  100. "_id":null,
  101. "max_projects":{$max:"$fundedProjects"},
  102. "debitors":
  103. {
  104. $addToSet:{
  105. "_id":"$_id",
  106. "name":"$name",
  107. "fundedAmount":"$fundedAmount",
  108. "fundedProjects":"$fundedProjects",
  109. "projects":"$projects",
  110.  
  111. }
  112. }
  113. }
  114. },
  115.  
  116. // Stage 9
  117. {
  118. $project: {
  119. "debitors":{
  120. $filter:
  121. {
  122. input:"$debitors",
  123. as:"deb",
  124. cond:{$eq:["$$deb.fundedProjects", "$max_projects"]}
  125. }
  126. }
  127. }
  128. },
  129.  
  130. // Stage 10
  131. {
  132. $unwind: {
  133. path : "$debitors"
  134. }
  135. },
  136.  
  137. // Stage 11
  138. {
  139. $project: {
  140. "_id":"$debitors._id",
  141. "name":"$debitors.name",
  142. "fundedAmount":"$debitors.fundedAmount",
  143. "fundedProjects":"$debitors.fundedProjects",
  144. "projects":"$debitors.projects"
  145. }
  146. },
  147.  
  148. // Stage 12
  149. {
  150. $sort: {
  151. "name":-1
  152. }
  153. },
  154.  
  155. // Stage 13
  156. {
  157. $out: "DebitorReport"
  158. },
  159.  
  160. ]
  161.  
  162. // Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/
  163.  
  164. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement