Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12231

MongoDB aggregation with lookup pipeline

$
0
0

My mongoDB statistical query is not performing as expected even after I aggregate all of the queries into a mongoDB aggregation. The final query result still contains an incorrect value.

[    {"$match": {"agencyId": ObjectId("649aedc8a827e7aed6ea7b79")  // Replace with the actual agency ID        }    },    {"$lookup": {"from": "OperationTransfer",  // Name of the transfer collection"localField": "_id",  // Field in operations referencing transfer document"foreignField": "operationId",  // Field in transfer collection"as": "transfer"        }    },    {"$unwind": {"path": "$transfer","preserveNullAndEmptyArrays": true  // Preserve operations without a transfer reference        }    },    {"$group": {"_id": "$status","count": { "$sum": 1 },"countWithBabySeats": {"$sum": {"$cond": [{ "$eq": ["$babySeats", true] }, 1, 0]                }            },"countInTruck": {"$sum": {"$cond": [{ "$eq": ["$inTruck", true] }, 1, 0]                }            },"countTransferWithNonEmptyOriginDestiny": {"$sum": {"$cond": [                        {"$and": [                                { "$ne": ["transfer", null] },                                { "$ne": ["transfer.origin", ""] },                                { "$ne": ["transfer.destiny", ""] }                            ]                        },                        1,                        0                    ]                }            }        }    }]

countTransferWithNonEmptyOriginDestiny in this stage, i can't check if i have the transfer object and if it contains the appropriate attribute with right values

I used $set to change the satge in order to update the result documents, but the values were still incorrect.


Viewing all articles
Browse latest Browse all 12231

Trending Articles