The problem of sorting mongodb subdocuments

  mongodb, question
Array(
    [id] => 00008
    [game] => Array
        (
            [0] => Array
                (
                    [gid] => 02569
                    [date] => 1363583244
                )

            [1] => Array
                (
                    [gid] => 04001
                    [date] => 1363339740
                )
        )
)
Array(
    [id] => 00010
    [game] => Array
        (
            [0] => Array
                (
                    [gid] => 02569
                    [date] => 1363583244
                )

            [1] => Array
                (
                    [gid] => 04028
                    [date] => 1363889740
                )
        )
)

The above are two of the many results. My current requirement is to find gid = 02569 from all the results and sort them by DESC according to the corresponding date

This is achieved by using the aggregate framework (for reference only, not tested). Mongodb 2.2+ is available.

db.xxx.aggregate([
    {
        $match: {'game.gid': 02569}
    },
    {
        $sort: {$date: -1}
    }
])