2013-10-07 52 views
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" 
      } 
     }, 
    ] 
} 

我將如何查詢(用貓鼬)找到所有foobars的所有文件的最小值和最大值time值。從文檔它似乎像這樣的事情是做的方式,但是這是行不通的:

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

大概的Map/Reduce? – freakish

回答

3

$unwindfoobars你組之前:

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