translate this mysql into a couchdb view?

半城伤御伤魂 提交于 2019-12-11 06:55:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!