mongodb - Return min and max of a subdocument property -


suppose have following collection:

{     "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"             }         },     ] } 

how query (using mongoose) find min , max time value foobars documents. documentation seems way it, isn't working:

foomodel.aggregate({ $group: { _id: null, maxtime: { $max: '$foobars.time' }}}, callback); 

$unwind foobars before group:

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

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -