Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- // Step 1: Exclude specified fields from computeAsset and remove _id
- {
- $project: {
- "_id": 0, // Remove the _id field
- "createdBy": 0,
- "createdAt": 0,
- "updatedBy": 0,
- "updatedAt": 0,
- "stageNumber": 0,
- "stageName": 0,
- "pid": 0,
- "jobId": 0,
- "version": 0,
- "isActive": 0,
- "source": 0
- }
- },
- // Step 1.2: Move additionalMetadata.warrantyExpiryDate to top level and remove additionalMetadata
- {
- $addFields: {
- "warrantyExpiryDate": "$additionalMetadata.warrantyExpiryDate"
- }
- },
- {
- $project: {
- "additionalMetadata": 0 // Remove the additionalMetadata field
- }
- },
- // Step 2: Exclude documents where hardwareComputeType is "Hardware"
- {
- $match: {
- "hardwareComputeType": { $ne: "Hardware" }
- }
- },
- // Step 3: Join with locationAsset collection on assetLocationId
- {
- $lookup: {
- from: "locationAsset",
- localField: "assetLocationId",
- foreignField: "_id",
- as: "location"
- }
- },
- // Unwind the location array to simplify access to fields
- {
- $unwind: "$location"
- },
- // Step 4: Project specified fields from locationAsset
- {
- $addFields: {
- "siteCode": "$location.siteCode",
- "siteName": "$location.siteName",
- "address": "$location.address"
- }
- },
- {
- $project: {
- "location": 0 // Remove the location field as it's no longer needed
- }
- },
- // Step 5 & 6: Join with baseAsset collection where assetType is "HARDWARE" and remove specified fields
- {
- $lookup: {
- from: "baseAsset",
- let: { baseAssetId: "$baseAssetId" },
- pipeline: [
- {
- $match: {
- $expr: {
- $and: [
- { $eq: ["$_id", "$$baseAssetId"] },
- { $eq: ["$assetType", "HARDWARE"] }
- ]
- }
- }
- },
- // Step 2 in baseAsset: Remove name and warrantyExpiryDate
- {
- $project: {
- "name": 0,
- "warrantyExpiryDate": 0
- }
- }
- ],
- as: "baseAsset"
- }
- },
- // Unwind the baseAsset array
- {
- $unwind: "$baseAsset"
- },
- // Step 7: Project specified fields from baseAsset
- {
- $addFields: {
- "versionMakeModel": "$baseAsset.versionMakeModel",
- "companyName_base": "$baseAsset.companyName" // Use a different field name to avoid overwriting
- }
- },
- {
- $project: {
- "baseAsset": 0 // Remove the baseAsset field
- }
- },
- // Optional: If you want to handle companyName conflicts
- {
- $addFields: {
- "companyName": { $ifNull: ["$companyName_base", "$companyName"] }
- }
- },
- {
- $project: {
- "companyName_base": 0 // Remove the temporary companyName_base field
- }
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment