jQuery CouchDB - filter keys for view
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
});
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 :)
精彩评论