jQuery CouchDB - filter keys for view

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

I am using the javascript library which comes bundled with couchdb to query the database.

On a side note here is a good overview of the functionality it provides, with a lot of good examples.

It is possible to filter the results from a view by specifying the key values to return. This is easily done with a query string (documentation) but how do I do it with the javascript API?

This is how I am doing it with a query string (please note that the JSON portion of the key-value-pair would need to be HTML encoded):

http://localhost:5984/MyDocuments/_design/MyDesign/_view/MyView?key=["Michael","2011-08-01"] 

And this is my javascript without the query string portion of the filtering process applied.

$.couch.db("MyDocuments").view("MyDesign/MyView", {     success: function(data) {         console.log(data);     },     error: function(status) {         console.log(status);     },     reduce: false }); 

回答1:

I actually figured this one out while writing the question. It was reasonably easy but there are not a lot of examples online so therefore feel the need to take this opportunity to provide an example.

$.couch.db("MyDocuments").view("MyDesign/MyView", {     success: function(data) {         console.log(data);     },     error: function(status) {         console.log(status);     },     key: ['Michael','2011-08-02'],     reduce: false }); 

The "key" section is what you are looking for :)



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