meetjoshi

pipeline - text - hardwareDataset

Sep 26th, 2024 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. [
  2.  
  3. // Step 1: Exclude specified fields from computeAsset and remove _id
  4. {
  5. $project: {
  6. "_id": 0, // Remove the _id field
  7. "createdBy": 0,
  8. "createdAt": 0,
  9. "updatedBy": 0,
  10. "updatedAt": 0,
  11. "stageNumber": 0,
  12. "stageName": 0,
  13. "pid": 0,
  14. "jobId": 0,
  15. "version": 0,
  16. "isActive": 0,
  17. "source": 0
  18. }
  19. },
  20.  
  21. // Step 1.2: Move additionalMetadata.warrantyExpiryDate to top level and remove additionalMetadata
  22. {
  23. $addFields: {
  24. "warrantyExpiryDate": "$additionalMetadata.warrantyExpiryDate"
  25. }
  26. },
  27. {
  28. $project: {
  29. "additionalMetadata": 0 // Remove the additionalMetadata field
  30. }
  31. },
  32.  
  33. // Step 2: Exclude documents where hardwareComputeType is "Hardware"
  34. {
  35. $match: {
  36. "hardwareComputeType": { $ne: "Hardware" }
  37. }
  38. },
  39.  
  40. // Step 3: Join with locationAsset collection on assetLocationId
  41. {
  42. $lookup: {
  43. from: "locationAsset",
  44. localField: "assetLocationId",
  45. foreignField: "_id",
  46. as: "location"
  47. }
  48. },
  49.  
  50. // Unwind the location array to simplify access to fields
  51. {
  52. $unwind: "$location"
  53. },
  54.  
  55. // Step 4: Project specified fields from locationAsset
  56. {
  57. $addFields: {
  58. "siteCode": "$location.siteCode",
  59. "siteName": "$location.siteName",
  60. "address": "$location.address"
  61. }
  62. },
  63. {
  64. $project: {
  65. "location": 0 // Remove the location field as it's no longer needed
  66. }
  67. },
  68.  
  69. // Step 5 & 6: Join with baseAsset collection where assetType is "HARDWARE" and remove specified fields
  70. {
  71. $lookup: {
  72. from: "baseAsset",
  73. let: { baseAssetId: "$baseAssetId" },
  74. pipeline: [
  75. {
  76. $match: {
  77. $expr: {
  78. $and: [
  79. { $eq: ["$_id", "$$baseAssetId"] },
  80. { $eq: ["$assetType", "HARDWARE"] }
  81. ]
  82. }
  83. }
  84. },
  85. // Step 2 in baseAsset: Remove name and warrantyExpiryDate
  86. {
  87. $project: {
  88. "name": 0,
  89. "warrantyExpiryDate": 0
  90. }
  91. }
  92. ],
  93. as: "baseAsset"
  94. }
  95. },
  96.  
  97. // Unwind the baseAsset array
  98. {
  99. $unwind: "$baseAsset"
  100. },
  101.  
  102. // Step 7: Project specified fields from baseAsset
  103. {
  104. $addFields: {
  105. "versionMakeModel": "$baseAsset.versionMakeModel",
  106. "companyName_base": "$baseAsset.companyName" // Use a different field name to avoid overwriting
  107. }
  108. },
  109. {
  110. $project: {
  111. "baseAsset": 0 // Remove the baseAsset field
  112. }
  113. },
  114.  
  115. // Optional: If you want to handle companyName conflicts
  116. {
  117. $addFields: {
  118. "companyName": { $ifNull: ["$companyName_base", "$companyName"] }
  119. }
  120. },
  121. {
  122. $project: {
  123. "companyName_base": 0 // Remove the temporary companyName_base field
  124. }
  125. }
  126.  
  127. ]
  128.  
Advertisement
Add Comment
Please, Sign In to add comment