Search code examples
mongodbrestheartfreeboard

Get latest value from mongoDB via RESTHeart for freeboard


Im sending temperature data with timestamp into MongoDB. My document structure looks like this:

{
      "_id": ObjectId("57c974d705578f617f5491e3"),         
      "temperature" : 24,
      "timestamp" : "9/2/2016, 2:47:19 PM",
      "_msgid" : "7944eab7.86bb14"
}

and I want to get to freeboard the latest value of temperature via RESTHeart query. Was looking throught the documentation on restheart.org but with no result... How would the query look like? Any ideas? Thanks for replys...


Solution

  • If the _id is autogenerated then the last value is always the first returned from GET /db/coll.

    This is because Restheart sort result by _id descending by default and the _id contains date and time in the most significant bytes.

    So GET /db/coll?pagesize=1 does the trick.

    If you want to sort by your timestamp field I see a problem since it is a string (not actually a date field) with a format that does not allow to sort by it lexicographically (such as for instance "2016-08-04-1839")