Last inserted record for each device

I have records stored in mongodb this way

{
  "resource": [
    {
      "user": "usr-0001",
      "deviceid": 101,
      "fixtime": 1461485045000
    },
    {
      "user": "usr-0001",
      "deviceid": 102,
      "fixtime": 1461485068000
    },
    {
      "user": "usr-0002",
      "deviceid": 201,
      "fixtime": 1461485098000
    },
    {
      "user": "usr-0002",
      "deviceid": 202,
      "fixtime": 1461485128000
    },
    {
      "user": "usr-0002",
      "deviceid": 202,
      "fixtime": 1461485141000
    }
  ]
}

How can I get only the last inserted records for each device belonging to a particular user? If the user has 5 devices it should return only the last inserted (5) records (based on fixtime) for that user. The fixtime shown is unix time.

Something like what’s described here: http://stackoverflow.com/questions/16368162/retrieve-the-last-inserted-row-from-each-user-in-database

haha I guess questions on this forum rarely get answered :confounded:

Try using these options: ?filter=user%3D'usr-0002'&limit=1&order=fixtime%20DESC

I generated this just by playing around the API Docs.

Thanks a lot. I think I tried that but it gets a little trick because it has to return 2 records (as there are 2 devices). I could do this easily with a simple operator in Mongo but not sure how to fit that in here. My Data source is a MongoDB. On Mongo I could do it this way - to get the last sale date for each item

db.sales.aggregate(
   [
     { $sort: { item: 1, date: 1 } },
     {
       $group:
         {
           _id: "$item",
           lastSalesDate: { $last: "$date" }
         }
     }
   ]
)

OH! I misunderstood. Not sure about that one, but I’ll ask around.