benoitc / couchbeam

Apache CouchDB client in Erlang
Other
242 stars 113 forks source link

utility function to retrive a doc #12

Closed benbro closed 14 years ago

benbro commented 14 years ago

I often need to retrieve a single doc by a key because I don't know the doc id. The example below gets a view and a key and respond with nodoc or the Doc. It'll also be useful to have a similar function that respond with the doc as a json string to be used in websites.

get_doc(View, Key) -> ViewPid = couchbeam_db:query_view(tengas_db, View, [ {"key", list_tobinary(Key)}, {"limit", 1}]), {ok, {[,,{,Rows}]}} = couchbeam_view:fetchview(ViewPid), case Rows of [] -> nodoc; [{[,,{,Doc}]}] -> Doc end.

benoitc commented 14 years ago

iThis function seem to specific for your own use. Not all views return docs like it. But Having a function to only fetch one result would be useful I agree. I will try to make something like it. Thanks for the snippet anyway.

benoitc commented 14 years ago

function couchbeam_view:first/1 and /2 added in latest head. Thanks!