假设我有以下集合:
{
"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);
答案 0 :(得分:3)
$unwind
foobars
:
FooModel.aggregate(
{ $unwind: '$foobars' },
{ $group: { _id: null, maxTime: { $max: '$foobars.time' }}},
callback);