4

I have a cloudant DB where each document looks like:

{
  "_id": "2015-11-20_attr_00",
  "key": "attr",
  "value": "00",
  "employeeCount": 12,
  "timestamp": "2015-11-20T18:16:05.366Z",
  "epocTimestampMillis": 1448043365366,
  "docType": "attrCounts"
}

For a given attribute there is an employee count. As you can see I have a record for the same attribute every day. I am trying to create a view or index that will give me the latest record for this attribute. Meaning if I inserted a record on 2015-10-30 and another on 2015-11-10, then the one that is returned to me is just employee count for the record with timestamp 2015-11-10.

I have tried view, but I am getting all the entries for each attribute not just the latest. I did not look at indexes because I thought they do not get pre calculated. I will be querying this from client side, so having it pre calculated (like views are) is important.

Any guidance would be most appreciated. thank you

asgs
  • 3,928
  • 6
  • 39
  • 54
bluetea
  • 85
  • 1
  • 6

2 Answers2

8
  1. I created a test database you can see here. Just make sure your when you insert your JSON document into Cloudant (or CouchDB), your timestamps are not strings but JavaScript data objects:

    https://examples.cloudant.com/latestdocs/_all_docs?include_docs=true

  2. I built a search index like this (name the design doc "summary" and the search index "latest"): function (doc) { if ( doc.docType == "totalEmployeeCounts" && doc.key == "div") { index("division", doc.value, {"store": true}); index("timestamp", doc.timestamp, {"store": true}); } }

  3. Then here's a query that will return only the latest record for each division. Note that the limit value will apply to each group, so with limit=1, if there are 4 groups you will get 4 documents not 1.

    https://examples.cloudant.com/latestdocs/_design/summary/_search/latest?q=*:*&limit=1&group_field=division&include_docs=true&sort_field=-timestamp

Raj
  • 544
  • 2
  • 10
0

Indexing TimeStamp as a string is not recommended.

Reference: https://cloudant.com/blog/defensive-coding-in-mapindex-functions/#.VvRVxtIrJaT

I have the same problem. I converted the timestamp value to milliseconds (number) and then indexed that value.

var millis= Date.parse(timestamp);

index("millis",millis,{"store": false}); 

You can use the same query as Raj suggested but with the 'millis' field instead of the timestamp .

Sora
  • 409
  • 5
  • 6