I have a MongoDB collection with number of items. Multiple items have a same key vlue(srkey), but value of their timestamp is different. I want to to fetch all items by srkey with greatest timestamp. How to accomplish this with php MongoDB.
For example the my collection contains following items
-
collection
item1 : {srkey:a,timestamp:1358675171} item2 : {srkey:b,timestamp:1358675175} item3 : {srkey:a,timestamp:1358676849} item4 : {srkey:d,timestamp:1358675213} item5 : {srkey:b,timestamp:1358676111}
Output of fetch must be
item3 : {srkey:a,timestamp:1358676849}
item4 : {srkey:d,timestamp:1358675213}
item5 : {srkey:b,timestamp:1358676111}
Item 1 and 3 have same srkey, so the result contains only one of the them with greatest timestamp value.
You can use the new aggregation framework for this:
It should be the first of that sorted group since you are sorting in a DESC manner.