Get the latest WHOLE record by date in mongodb -


i have collection objects (records of events) this:

{_id: 1, type: "a", val2: "x", val3: "z", date: 1/1} {_id: 2, type: "a", val2: "y", val3: "y", date: 2/1} {_id: 3, type: "c", val2: "z", val3: "x", date: 3/1} {_id: 4, type: "b", val2: "x", val3: "z", date: 4/1} {_id: 5, type: "c", val2: "y", val3: "y", date: 5/1} {_id: 6, type: "b", val2: "z", val3: "x", date: 6/1} 

i fetch complete object latest date each type, in example above should return records ids: 2, 5, 6

i'm doing pipeline query this:

db.items.aggregate(    [      {        $group:          {            _id: "$type",            lastdate: { $last: "$date" }          }      }    ] ) 

but returns me documents this:

{ _id: 2, lastdate: 2/1} 

whereas want entire object (with val2, val3, etc)

how can accomplish this?

thanks

you can specific items

db.items.aggregate(  [    {      $group:        {          _id: "$type",          lastdate: { $last: "$date" },          val2: { $last: "$val2" },          val3: { $last: "$val3" }        }    }  ] ) 

note should use isodate type date , time fields.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -