Reputation: 16339
My design document is something like this..
{
"_id": "_design/dataFilter",
"_rev": "1-29032a3479f5bfb016ea4e2f5e8afae3",
"filters": {
"getUser": "function (doc, req) { return (req.query.type == doc.type && req.query.id == doc._id) }",
"getCustomer": "function (doc, req) { return (req.query.type == doc.type && req.query.id == doc.user) }",
"getOrders": "function (doc, req) { return (req.query.type == doc.type && req.query.id == doc.user) }",
"getCloths": "function (doc, req) { return (req.query.id == doc._id) }",
"getDevices": "function (doc, req) { return (req.query.type == doc.type && req.query.id == doc._id) }"
}
}
This is how I am trying to fetch the records from couchdb.
curl -X GET http://example.com/database?filter=dataFilter/getCustomer -d '{"query_params":{"id":"jayesh","type":"user"}}'
How can we fetch records from the couchdb with the help of design document?
Upvotes: 0
Views: 950
Reputation: 56
Regarding the documentation of couchdb, filters can only be used for the _changes
feed:
http://docs.couchdb.org/en/2.0.0/couchapp/ddocs.html#filter-functions
What you can do, is using views with map/reduce functions: http://docs.couchdb.org/en/2.0.0/couchapp/ddocs.html#view-functions
You just have to emit
all documents that match the filter. The key and the value of the emit
-function can be used as you want.
In your example, the design-doc would look like this:
This would look like this:
{
"_id": "_design/dataFilter",
"_rev": "1-29032a3479f5bfb016ea4e2f5e8afae3",
"views": {
"getUser": {
"map": "function(doc) { if (req.query.type == doc.type && req.query.id == doc._id) { emit(doc._id, "something") } }"
},
...
}
}
After the view has been computed (what could take some time on big databases) you can fetch the results with:
GET DB/_design/dataFilter/_view/getUser
Upvotes: 1