Group stage skipping on fields

Hello, I am running the following mongodb aggregation pipeline query:

var pipeline = [
        {
            "$match": {
                "shipmentDate": {
                    "$gte": ISODate("2023-10-02 00:00:00.000+02:00")
                    "$lte": ISODate("2023-10-23 00:00:00.000+02:00")
                }
            }
        }, 
        {
            "$lookup": {
                "from": "bags",
                "localField": "bag",
                "foreignField": "_id",
                "as": "bags"
            }
        }, 
        {
            "$match": {
                "bags": {
                    "$exists": true,
                    "$ne": []
                }
            }
        }, 
        {
            "$unwind": {
                "path": "$bags"
            }
        }, 
        {
            "$group": {
                "_id": {
                    "zipcode": "$address.zipcode",
                    "categoryId": "$categoryDetails.categoryId",
                    "categoryName": "$categoryDetails.name",
                    "courierId": "$bags.courier.userId",
                    "name": "$bags.courier.name"
                },
                "count": {
                    "$sum": 1
                },
                "totalValue": {
                    "$sum": "$priceData.price"
                }
            }
        }, 
        {
            "$project": {
                "_id": 0,
                "courier": "$_id.name",
                "zipcode": "$_id.zipcode",
                "category": "$_id.categoryName",
                "count": "$count",
                "totalValue": "$totalValue"
            }
        }, 
        {
            "$sort": {
                "courier": 1
            }
        }
    ];

There is a problem with the courier field- it is never displayed in the output (even though it is projected). After disabling stages 1 by 1 I have tracked the issue down to the group stage. After this stage the output doesn’t contain the courier details (nor courierId, nor name). What could be causing these issues? It needs to be said that bags.courier is potentially null, could that be breaking the grouping?

Got a sample document?

@John_Sewell Hello, thank you for your reply. Here is a sample BAG document that gets $lookup’ed but name does not get projected in the end. The “courier” subdocument is optional and may not exist.

{
    "_id" : ObjectId("653697dc72a5ebd93f88715e"),
    "courier" : {
        "name" : "John Doe",
        "email" : "john@doe.com",
        "cell" : "243244",
        "gender" : "MALE",
        "userId" : ObjectId("642ce61139d0a2b1ca526d97"),
        "isDelivery" : true,
        "_id" : ObjectId("653697dc72a5ebd93f88715f")
    },
    "creationDate" : ISODate("2023-10-23T15:57:16.965+0000"),
    "assignmentDate" : ISODate("2023-10-23T22:00:00.000+0000"),
    "__v" : 0
}

That’s weird, I just tested it and it worked, I created a sample document to lookup to bags and removed the dates and filtering:

If you have a pair of documents that don’t work, can you create a playground with them in?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.