Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [
- // Step 1: Lookup with locationAsset
- {
- "$lookup": {
- "from": "locationAsset",
- "localField": "assetLocationId",
- "foreignField": "_id",
- "as": "locationInfo"
- }
- },
- // Step 2: Unwind locationInfo array
- {
- "$unwind": {
- "path": "$locationInfo",
- "preserveNullAndEmptyArrays": true
- }
- },
- // Step 3: Lookup with baseAsset
- {
- "$lookup": {
- "from": "baseAsset",
- "localField": "baseAssetId",
- "foreignField": "_id",
- "as": "baseInfo"
- }
- },
- // Step 4: Unwind baseInfo array
- {
- "$unwind": {
- "path": "$baseInfo",
- "preserveNullAndEmptyArrays": true
- }
- },
- // Step 5: Use addFields to include fields from baseInfo that are missing in the root document
- {
- "$addFields": {
- "baseFields": {
- "$arrayToObject": {
- "$filter": {
- "input": { "$objectToArray": "$baseInfo" },
- "as": "baseField",
- "cond": { "$not": { "$in": ["$$baseField.k", { "$objectToArray": "$$ROOT" }] } }
- }
- }
- }
- }
- },
- // Step 6: Add fields from both computeAsset and baseAsset into a single document
- {
- "$addFields": {
- "mergedFields": {
- "$mergeObjects": ["$$ROOT", "$baseFields"]
- }
- }
- },
- // Step 7: Project the final fields you want to output
- {
- "$project": {
- "finalDocument": "$mergedFields"
- }
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment