Joelbitar
Joelbitar

Reputation: 3595

How do I keep documents in aggregation with $unwind

Lets say I have three students...

Alice, she is Always there on fridays.

{
    "name" : "Alice",
    "goes" : {
        "mondays" : {
            "fr" : 900,
            "to" : 1400
        },
        "fridays" : {
            "fr" : 700,
            "to" : 1600
        },
    }
}

And bob, here should be there on the first of january

{
    "_id" : ObjectId("5284a7085d60338b40b8f17d"),
    "name" : "Bob",
    "goes" : {
        "mondays" : {
            "fr" : 800,
            "to" : 1200
        },
        "special" : [ 
            {
                "date" : "2010-01-01",
                "fr" : 1000,
                "to" : 1500
            }
        ]
    }
}

And Clair who will not be attenging on mondays or at 10.00

{
    "_id" : ObjectId("5284c2785d60338b40b8f17f"),
    "name" : "Clair",
    "goes" : {
        "wednesdays" : {
            "fr" : 1100,
            "to" : 1500
        },
        "special" : [ 
            {
                "date" : "2010-01-01",
                "fr" : 1600,
                "to" : 1900
            }, 
            {
                "date" : "2010-01-02",
                "fr" : 1000,
                "to" : 1300
            }
        ]
    }
}

I want to find all students that should attend on fridays at 7 och 10 on the first of January 2010

So I do this with the aggregation framework.

db.students.aggregate(
    [
        {
            $unwind: "$goes.special"
        },
        {
            $match: {
                $or : [
                    {
                        'goes.fridays.fr': 700,
                    },
                    {
                        'goes.special.date' : '2010-01-01',
                        'goes.special.fr': 1000
                    }
                ]
            }
        }
    ]
)

But Alice does not show up. It clearly states why in the mongodb docs, http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/ at the very bottom.

"If you specify a target field for $unwind that holds an empty array ([]) in an input document, the pipeline ignores the input document, and will generates no result documents."

I could solve it by adding an array with a null value in it but that does not seam like a nice solution.

Is there a way I could get unwind NOT to ignore documents that does not have data in a $unwind'ed array?

Upvotes: 0

Views: 853

Answers (1)

zero323
zero323

Reputation: 330063

You don't need $unwind at all. Simple $match in pipeline is enough:

pipeline = [
    {
        "$match" : {
            "$or" : [
                {
                    "goes.fridays.fr" : 700
                },
                {
                    "goes.special" : {
                        "$elemMatch" : {
                            "date" : "2010-01-01",
                            "fr" : 1000
                        }
                    }
                }
            ]
        }
    }
]

db.students.aggregate(pipeline)

It can be done easily even without aggregation framework.

query = {
    "$or" : [
        {
            "goes.fridays.fr" : 700
        },
        {
            "goes.special" : {
                "$elemMatch" : {
                    "date" : "2010-01-01",
                    "fr" : 1000
                }
            }
        }
    ]
}

db.students.find(query)

Upvotes: 1

Related Questions