Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. mongoose.aggregate([
  2. { "$match": { "weekNumber": weekNumber } },
  3. { "$unwind": "$locations" },
  4. { "$addFields": { "shifts": "$locations.shifts"}},
  5. { "$unwind": "$shifts" },
  6. { "$lookup": {
  7. "let": { "userObjId": { "$toObjectId": "$shifts.shiftTypeId" } },
  8. "from": "shiftTypes",
  9. "pipeline": [
  10. { "$match" : { "$expr": { "$eq" : [ "$_id", "$$userObjId"] } } }
  11. ],
  12. "as": "shiftType"
  13. }
  14. },
  15. { "$addFields": {
  16. "shifts.name": "$shiftType.name",
  17. "shifts.color": "$shiftType.color"
  18. }
  19. },
  20. { "$project": {
  21. "shiftType": 0,
  22. "locations.shifts": 0
  23. }
  24. }
  25. ])
  26.  
  27. [
  28. {
  29. _id: ObjectId(),
  30. "weekNumber": 30,
  31. "locations": [
  32. {
  33. "location": "locationName1",
  34. "shifts": [
  35. {
  36. "shiftTypeId": "shiftType001",
  37. },
  38. {
  39. "shiftTypeId": "shiftType002",
  40. },
  41. {
  42. "shiftTypeId": "shiftType001",
  43. }
  44. ]
  45. },
  46. {
  47. "location": "locationName2",
  48. "shifts": [
  49. {
  50. "shiftTypeId": "shiftType001",
  51. },
  52. {
  53. "shiftTypeId": "shiftType002",
  54. },
  55. {
  56. "shiftTypeId": "shiftType001",
  57. }
  58. ]
  59. },
  60. {
  61. "location": "locationName3",
  62. "shifts": [
  63. {
  64. "shiftTypeId": "shiftType001",
  65. },
  66. {
  67. "shiftTypeId": "shiftType002",
  68. },
  69. {
  70. "shiftTypeId": "shiftType001",
  71. }
  72. ]
  73. }
  74. ]
  75. }
  76. ]
  77.  
  78. [
  79. {
  80. _id: ObjectId("shiftType001"),
  81. "name": "shift001",
  82. "color": "red"
  83. },
  84. {
  85. _id: ObjectId("shiftType002"),
  86. "name": "shift002",
  87. "color": "blue"
  88. }
  89. ]
  90.  
  91. [
  92. {
  93. _id: ObjectId(),
  94. "weekNumber": 30,
  95. "locations": [
  96. {
  97. "location": "locationName1",
  98. "shifts": [
  99. {
  100. "shiftTypeId": "shiftType001",
  101. "name": "shift001",
  102. "color": "red"
  103. },
  104. {
  105. "shiftTypeId": "shiftType002",
  106. "name": "shift002",
  107. "color": "blue"
  108. },
  109. {
  110. "shiftTypeId": "shiftType001",
  111. "name": "shift001",
  112. "color": "red"
  113. }
  114. ]
  115. },
  116. {
  117. "location": "locationName2",
  118. "shifts": [
  119. {
  120. "shiftTypeId": "shiftType002",
  121. "name": "shift002",
  122. "color": "blue"
  123. },
  124. {
  125. "shiftTypeId": "shiftType002",
  126. "name": "shift002",
  127. "color": "blue"
  128. },
  129. {
  130. "shiftTypeId": "shiftType001",
  131. "name": "shift001",
  132. "color": "red"
  133. }
  134. ]
  135. },
  136. {
  137. "location": "locationName3",
  138. "shifts": [
  139. {
  140. "shiftTypeId": "shiftType001",
  141. "name": "shift001",
  142. "color": "red"
  143. },
  144. {
  145. "shiftTypeId": "shiftType001",
  146. "name": "shift001",
  147. "color": "red"
  148. },
  149. {
  150. "shiftTypeId": "shiftType001",
  151. "name": "shift001",
  152. "color": "red"
  153. }
  154. ]
  155. }
  156. ]
  157. }
  158. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement