0

I have a collection :

{ "_id" : 1, "item" : "abc", "date" : ISODate("2014-01-01T08:00:00Z"), "price" : 10, "quantity" : 2 }
{ "_id" : 2, "item" : "jkl", "date" : ISODate("2014-02-03T09:00:00Z"), "price" : 20, "quantity" : 1 }
{ "_id" : 3, "item" : "xyz", "date" : ISODate("2014-02-03T09:05:00Z"), "price" : 5, "quantity" : 5 }
{ "_id" : 4, "item" : "abc", "date" : ISODate("2014-02-15T08:00:00Z"), "price" : 10, "quantity" : 10 }
{ "_id" : 5, "item" : "xyz", "date" : ISODate("2014-02-15T09:05:00Z"), "price" : 5, "quantity" : 10 }
{ "_id" : 6, "item" : "xyz", "date" : ISODate("2014-02-15T12:05:10Z"), "price" : 5, "quantity" : 5 }
{ "_id" : 7, "item" : "xyz", "date" : ISODate("2014-02-15T14:12:12Z"), "price" : 5, "quantity" : 10 }
{ "_id" : 7, "item" : "xyz", "date" : ISODate("2014-02-15T17:12:12Z"), "price" : 5, "quantity" : 10 }
{ "_id" : 7, "item" : "pop", "date" : ISODate("2014-02-16T14:12:12Z"), "price" : 5, "quantity" : 10 }
{ "_id" : 7, "item" : "pop", "date" : ISODate("2014-02-10T14:12:12Z"), "price" : 5, "quantity" : 10 }

I want to fetch the last entry based on the date for a given condition ... let's say :

I would like to get all the last entry for the item ["abc","pop"] using $in operator at once.

The result would be : 

{ "_id" : 4, "item" : "abc", "date" : ISODate("2014-02-15T08:00:00Z"), "price" : 10, "quantity" : 10 }
{ "_id" : 7, "item" : "pop", "date" : ISODate("2014-02-16T14:12:12Z"), "price" : 5, "quantity" : 10 }
  • Does this answer your question: https://stackoverflow.com/questions/29429472/latest-record-by-date-for-each-item-mongodb-group – vishnu Jan 23 '21 at 12:56
  • Yeah, got the logic just need to tweak the query. Thanks –  Jan 23 '21 at 14:41

0 Answers0