问题
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!
回答1:
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.
来源:https://stackoverflow.com/questions/4164203/translate-this-mysql-into-a-couchdb-view