Couchdb: Read a View in Java

941 Views Asked by At

I am using couchdb for first time but I am facing a problem that I cannot find how to solve. I am creating an ad-hoc view with the following line

ViewResults resultAdHoc = db.adhoc("function(doc) {emit(null, doc.name);}");

when I am running the function with futon I am getting the names in the value side, so I guess it is correctly written. The form of one document is the following

{
   "_id": "d11d7fa59d162658b7cc95c34a001ce0",
   "_rev": "1-a4038d7061988c7552f9b8b435bba9bf",
   "name": "MyName",
   "session": "549C6567BE25D96EA1D2553C4A9DE175"
}

This that I cannot figure out is how to read this so I could get all the names of the documents in Java for further processing (e.g. to print them);

p.s. I am using the CouchDB4J

1

There are 1 best solutions below

3
Slartibartfast On

I haven't tried this, but something like this should work

ViewResults resultAdHoc = db.adhoc("function(doc) {emit(null, doc.name);}");
for (Document d: resultAdHoc.getResults()) {
    String name = d.getString("value");
    ....
}