0

假设我有以下集合:

{
    "title": "foo1",
    "description: "bar1",
    "foobars": [
        {
            "title": "foobar1",
            "time": {
                "$date": "2013-10-04T19:53:54.714Z"
            }
        },
        {
            "title": "foobar2",
            "time": {
                "$date": "2013-10-06T19:53:54.714Z"
            }
        },
    ]
}

我将如何查询(使用 Mongoose)来查找所有文档的最小值和time最大值foobars。从文档看来,这样做似乎是这样的,但这不起作用:

FooModel.aggregate({ $group: { _id: null, maxTime: { $max: '$foobars.time' }}}, callback);
4

1 回答 1

3

$unwind foobars在你分组之前:

FooModel.aggregate(
    { $unwind: '$foobars' },
    { $group: { _id: null, maxTime: { $max: '$foobars.time' }}}, 
    callback);
于 2013-10-07T14:04:37.610 回答