0
votes

i have connected nodered to Watson iot platform using raspberry pi, now Watson extends to Cloudant NOSQL databse, the datas are arranged in random manner.When i send getrequest from external app the datas are random, how do i fetch data in order according to timestamp?

1

1 Answers

1
votes

Assuming your documents contain a field with a timestamp value you could create a view and query it (https://console.ng.bluemix.net/docs/services/Cloudant/api/creating_views.html#views-mapreduce-).

CouchDB/Cloudant returns view results sorted by the key that's defined in the view definition. Simple example, assuming all docs include a field named date_sent representing a timestamp value:

{
    ...
    "date_sent": "2017-02-09T21:37:20.731Z",
    ...
}
  • Create a view (replace the placeholders $... as appropriate)

    PUT https://$USERNAME:$PASSWORD@$USERNAME.cloudant.com/$DATABASE/_design/$DD_NAME HTTP/1.1
    
    {
     "views" : {
       "docs_sorted_by_date" : {
        "map" : "function(doc) { emit(doc.date_sent,1);  }"
       }
     }
    }
    
  • Query the view

    GET https://$USERNAME:$PASSWORD@$USERNAME.cloudant.com/$DATABASE/_design/$DD_NAME/_view/docs_sorted_by_date HTTP/1.1