Question

I'm quite new with couchdb and I want to create a view based on a simple mysql statement. I found this documentation: http://guide.couchdb.org/draft/cookbook.html but there are sadly not all use cases included.

My MySQL-Statement:

SELECT `title`, `id`, `author`, `date`, `text` FROM `news` WHERE `date`<=NOW() AND `author`='22' ORDER BY `date` DESC LIMIT 20,10;

Thank you very much!

Was it helpful?

Solution

You need to write a view with the following map function.

function(doc) {
    emit([doc.author, doc.date], {
            "title": doc.title, 
            "author": doc.author, 
            "date": doc.date, 
            "text": doc.text});
}

Now you can query the view using the following URL:

http://127.0.0.1:5984/dbname/_design/design_doc_name/_view/viewname?startkey=[22, "2010-11-12T10:20:30"]&endkey=[22, {}]&descending=true&skip=20&limit=10

The date in the start key must be the current datetime. There is no way to emulate NOW() in couchdb.

A view in couchdb is just a list of key-value pairs sorted by key and it provides a way to access a range of that list. You need to design your view such that you can get the results that you need using a range query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top