mathieuancelin / play2-couchbase

Couchbase plugin for Play framework 2.2 with reactive API
http://reactivecouchbase.org/
19 stars 8 forks source link

Question : Usage of query parameter #15

Closed damageco closed 11 years ago

damageco commented 11 years ago

Hello,

I've been successful with my first try to use the CRUD Controller, I can create, get, update, delete and list documents, but I didn't find the right syntax to use the query parameter in the find and stream functions to filter results. Can you please provide an example ?

Thanks.

David

mathieuancelin commented 11 years ago

Hi,

I can understand it can be difficult to use find without proper documentation :-)

first, find is a POST request with a Json object body, this object can be something like :

{
    "docName":"theDocumentName", // optional, by default value is extracted from the CRUD controller
    "view":"theViewName", // optional, by default value is extracted from the CRUD controller
    "q":"theValueI'mLookingFor", // does a couchbase query with from:theValueI'mLookingFor and to: theValueI'mLookingFor\uefff 
    "from":"a", // optional, overrides the range start set by q on the couchbase query
    "to":"c", // optional, overrides the range stop set by q on the couchbase query
    "limit":100, // optional, default is unlimited
    "descending":false, // optional, default is couchbase default
    "skip":0 // optional, skip some results
}

if you're looking for a beer named Budweiser, just call find with :

{
    "docName":"beers", 
    "view":"byName", 
    "q":"budweiser"
}

of course you need a beersdesign document on your couchbase server with a byNameview that extracts beers name.

function (doc, meta) { 
  if (doc.datatype === 'beer') { 
    emit(doc.name, null); 
  } 
}

for the stream version, it's the same, but with query params instead of Json object (query params are named like json fields)

damageco commented 11 years ago

Yes, it's always better with a good documentation ;-)

I've succeed with this syntax :

curl -i -X POST "http://localhost:9000/mails/find/?doc=mails&view=by_from&q=david&limit=1"HTTP/1.1 200 OK

reply :

Content-Type: application/json; charset=utf-8
Content-Length: 154

[{"from":"david@gmail.com","to":"jocelyne@gmail.com","subject":"tests envoi mail modifié","docType":"mail","_id":"10b7fc19-7817-4265-ad43-ef53928a91c7"}]

but I've got an error (Invalid JSON) with this syntax :

curl -i --header "Content-Type:application/json" -X POST -d {"docName":"mails","view":"by_from", "q":"david"} "http://localhost:9000/mails/find"

... I think it's a silly mistake from me but can't see it for now.

mathieuancelin commented 11 years ago

Maybe you have to put the Json data between simple quotes ?

curl -i --header "Content-Type:application/json" -X POST -d '{"docName":"mails","view":"by_from", "q":"david"}' "http://localhost:9000/mails/find"
damageco commented 11 years ago

That's it, thanks !