Praneeth Vadlakonda
Praneeth Vadlakonda

Reputation: 37

Get selected latest 10 docs from couchDB order by timestamp

When I run the below view I am getting docs order by time. But if I call URL like below by passing specific device_sno its not working.....

View :

function(doc){
    emit([doc.device_sno,doc.timestamp],doc);
}

URL :

http://localhost:5984/trip_test/_design/test/_view/test?key=["Axe001"]&limit=10&ascending=true&include_docs=true

I need to get last 10 records whose device_sno is Axe001 order by timestamp!

Can anyone help me to solve this issue, it would be great help to me. Thanks in advance!

Upvotes: 0

Views: 281

Answers (1)

TheDude
TheDude

Reputation: 3952

Try this:

http://localhost:5984/trip_test/_design/test/_view/test?start_key=["Axe001"]&limit=10&end_key=["Axe001", {}]

That will give you the first 10 items. To get the last 10, do:

http://localhost:5984/trip_test/_design/test/_view/test?end_key=["Axe001"]&limit=10&start_key=["Axe001", {}]&descending=true

Putting key would force it to match that specific key, which isn't the case because the keys have two elements.

Upvotes: 3

Related Questions