meetjoshi

pipeline - old - mongodb - hardwareDataset

Sep 26th, 2024 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 1.43 KB | None | 0 0
  1. [
  2.   // Step 1: Lookup with locationAsset
  3.   {
  4.     "$lookup": {
  5.       "from": "locationAsset",
  6.       "localField": "assetLocationId",
  7.       "foreignField": "_id",
  8.       "as": "locationInfo"
  9.     }
  10.   },
  11.   // Step 2: Unwind locationInfo array
  12.   {
  13.     "$unwind": {
  14.       "path": "$locationInfo",
  15.       "preserveNullAndEmptyArrays": true
  16.     }
  17.   },
  18.   // Step 3: Lookup with baseAsset
  19.   {
  20.     "$lookup": {
  21.       "from": "baseAsset",
  22.       "localField": "baseAssetId",
  23.       "foreignField": "_id",
  24.       "as": "baseInfo"
  25.     }
  26.   },
  27.   // Step 4: Unwind baseInfo array
  28.   {
  29.     "$unwind": {
  30.       "path": "$baseInfo",
  31.       "preserveNullAndEmptyArrays": true
  32.     }
  33.   },
  34.   // Step 5: Use addFields to include fields from baseInfo that are missing in the root document
  35.   {
  36.     "$addFields": {
  37.       "baseFields": {
  38.         "$arrayToObject": {
  39.           "$filter": {
  40.             "input": { "$objectToArray": "$baseInfo" },
  41.             "as": "baseField",
  42.             "cond": { "$not": { "$in": ["$$baseField.k", { "$objectToArray": "$$ROOT" }] } }
  43.           }
  44.         }
  45.       }
  46.     }
  47.   },
  48.   // Step 6: Add fields from both computeAsset and baseAsset into a single document
  49.   {
  50.     "$addFields": {
  51.       "mergedFields": {
  52.         "$mergeObjects": ["$$ROOT", "$baseFields"]
  53.       }
  54.     }
  55.   },
  56.   // Step 7: Project the final fields you want to output
  57.   {
  58.     "$project": {
  59.       "finalDocument": "$mergedFields"
  60.     }
  61.   }
  62. ]
  63.  
Advertisement
Add Comment
Please, Sign In to add comment