Guest User

Untitled

a guest
Dec 1st, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. // [
  2. // {
  3. // _id:"5be8d00dd98d9b11ecfe7231",
  4. // nombreCom:"Transitor",
  5. // referenciaCom: "2N2222",
  6. // tipo:"NPN",
  7. // empaque:"TO92",
  8. // cantidad:20
  9. // },
  10. // {
  11. // _id:"5be8d00dd98d9b11ecf12038",
  12. // nombreCom:"Transitor",
  13. // referenciaCom: "Tip31c",
  14. // tipo:"NPN",
  15. // empaque:"TO220",
  16. // cantidad:10
  17. // },
  18. // {
  19. // _id:"5be8d00dd98d9b11ecf4a2d4",
  20. // nombreCom:"Transitor",
  21. // referenciaCom: "2N3906",
  22. // tipo:"PNP",
  23. // empaque:"TO92",
  24. // cantidad:10
  25. // },
  26. // {
  27. // _id:"5be8d00dd98d9b11ecf1b2b3",
  28. // nombreCom:"condensador",
  29. // referenciaCom: "22 Mf",
  30. // tipo:"electrolitico",
  31. // empaque:"N/A",
  32. // cantidad:24
  33. // },
  34. // {
  35. // _id:"5be8d00dd98d9b11ecf5c4c3",
  36. // nombreCom:"condensador",
  37. // referenciaCom: "0.1 Nf - 104",
  38. // tipo:"ceramico",
  39. // empaque:"N/A",
  40. // cantidad:79
  41. // },
  42. // {
  43. // _id:"5be8d00dd98d9b11ecfc37d1",
  44. // nombreCom:"cable",
  45. // referenciaCom: "AWG 12",
  46. // tipo:"Homologado",
  47. // empaque:"N/A",
  48. // cantidad:230
  49. // },
  50. // {
  51. // _id:"5be8d00dd98d9b11ecfe4a21",
  52. // nombreCom:"cable",
  53. // referenciaCom: "AWG 18",
  54. // tipo:"Sin Homologar",
  55. // empaque:"N/A",
  56. // cantidad:530
  57. // }
  58. // ]
  59.  
  60. // [
  61. // {
  62. // _id:"5be8d00dd98d9b11ecf1d20",
  63. // nombres:"Andres",
  64. // apellidos: "Jimenez Roa",
  65. // reporteUso:[
  66. // {
  67. // _id:"5be8d00dd98d9b11ecf9d2a1",
  68. // componenteUsado:[
  69. // "5be8d00dd98d9b11ecfe7231",
  70. // "5be8d00dd98d9b11ecf4a2d4",
  71. // "5be8d00dd98d9b11ecf5c4c3"
  72. // ],
  73. // fechaUso:"..la Fecha.."
  74. // },
  75. // {
  76. // _id:"5be8d00dd98d9b11ecf00321",
  77. // componenteUsado:[
  78. // "5be8d00dd98d9b11ecfc37d1"
  79. // ],
  80. // fechaUso:"..la Fecha.."
  81. // }
  82. // ],
  83. // emailUser:"andres@xyz.com",
  84. // password:"*********"
  85. // },
  86. // {
  87. // _id:"5be8d00dd98d9b11ecd3a12",
  88. // nombres:"Felipe",
  89. // apellidos: "Castro Roa",
  90. // reporteUso:[], //NADA ALMACENADO
  91. // emailUser:"felipe@xyz.com",
  92. // password:"*********"
  93. // },
  94. // {
  95. // _id:"5be8d00dd98d9b11ecd3a12",
  96. // nombres:"Driana",
  97. // apellidos: "Perez Gacha",
  98. // reporteUso:[
  99. // {
  100. // _id:"5be8d00dd98d9b11ecf9d2a1",
  101. // componenteUsado:[
  102. // "5be8d00dd98d9b11ecfc37d1",
  103. // "5be8d00dd98d9b11ecfe4a21"
  104. // ],
  105. // fechaUso:"..la Fecha.."
  106. // },
  107. // {
  108. // _id:"5be8d00dd98d9b11ecf00321",
  109. // componenteUsado:[
  110. // "5be8d00dd98d9b11ecfc37d1",
  111. // "5be8d00dd98d9b11ecfe4a21"
  112. // ],
  113. // fechaUso:"..la Fecha.."
  114. // }
  115. // ],
  116. // emailUser:"driana@xyz.com",
  117. // password:"*********"
  118. // }
  119. // ]
  120.  
  121. //===================================
  122. //definir mongoose schemas
  123. let sch_Componente = {
  124. //..se define el schema...
  125. };
  126.  
  127. let sch_Trabajador = {
  128. //..se define el schema...
  129. };
  130. //===================================
  131. //definir modelos mongoose
  132. //no pluralizo los nombres de colecciones
  133. let modelComponente = mongoose.model("componente", sch_Componente);
  134. let modelTrabajador = mongoose.model("trabajador", sch_Trabajador);
  135. //===================================
  136. //cursor de agregacion:
  137. let cursorAgg = modelTrabajador.aggregate([
  138.  
  139. //busco por el _id del trabajador Andres Jimenez Roa
  140. //(convierto el string _id en ObjetoId )
  141. {$match : { _id : mongoose.Types.ObjectId("5be8d00dd98d9b11ecf1d20") }},
  142.  
  143. //aplano el array de reporteUso:
  144. {$unwind : "$reporteUso"},
  145.  
  146. //busco el _id del reporte de uso
  147. {$match : { "reporteUso._id" : mongoose.Types.ObjectId("5be8d00dd98d9b11ecf9d2a1") }},
  148.  
  149. //Une los documentos con la coleccion componente
  150. {$lookup:
  151. {
  152. from: "componente",
  153. localField: "reporteUso.componenteUsado",
  154. foreignField: "_id",
  155. as: "reporteUso.componenteUsado"
  156. }
  157. },
  158.  
  159. //aplanar el array de componenteUsado
  160. {$unwind:"$reporteUso.componenteUsado"},
  161.  
  162. //Busco el empaque
  163. {$match:{"reporteUso.componenteUsado.empaque": "TO92"}},
  164.  
  165. //----falta rearmar y proyectar---------
  166. //{$group:{_id:"$_id", reporteUso:{$push:"$reporteUso"}}},
  167. //{$project:{_id:1, nombres:1, apellidos:1, reporteUso:1, componenteUsado:1}}
  168. ]);
  169.  
  170. //===================================
  171.  
  172. // {
  173. // _id:"5be8d00dd98d9b11ecf1d20",
  174. // nombres:"Andres",
  175. // apellidos: "Jimenez Roa",
  176. // reporteUso:[
  177. // {
  178. // _id:"5be8d00dd98d9b11ecf9d2a1",
  179. // componenteUsado:[
  180. // {
  181. // _id:"5be8d00dd98d9b11ecfe7231",
  182. // nombreCom:"Transitor",
  183. // referenciaCom: "2N2222",
  184. // tipo:"NPN",
  185. // empaque:"TO92",
  186. // cantidad:20
  187. // },
  188. // {
  189. // _id:"5be8d00dd98d9b11ecf4a2d4",
  190. // nombreCom:"Transitor",
  191. // referenciaCom: "2N3906",
  192. // tipo:"PNP",
  193. // empaque:"TO92",
  194. // cantidad:10
  195. // }
  196. // ],
  197. // fechaUso:"..la Fecha.."
  198. // }
  199. // ]
  200. // }
Add Comment
Please, Sign In to add comment