Fetch views

Skip to end of metadata
Go to start of metadata

Value is an Array

Map function:

function(doc) {
  if(doc.country_id){
    emit(doc.country.code, [doc.country_id, doc.country.code]);
  }
}

The following CouchDB view result:

{"total_rows":959,"offset":0,"rows":[
{"id":"c82c58ae07c5383ff8213d80eb854755","key":"ae","value":["4","ae"]},
{"id":"04d6113249622e733c70faf90262c76c","key":"ar","value":["96","ar"]},
{"id":"0b36d680c324c47df9f4efa646a83e07","key":"ar","value":["96","ar"]},

can be queried using the fetchView method:

CouchDbClient client = new DefaultCouchDbClient();
Database database = client.getDatabase("dropzones");
ViewResult byCountry = database.fetchView(ViewQuery.builder("dropzone/by_country").build());
for (ViewResultRow row : byCountry) {
    //System.out.println(row);
    System.out.println(row.getKey());
    JSONArray ary = row.getValueAsArray();
    System.out.println(ary);
}

prints out:

ae
["4","ae"]
ar
["96","ar"]
ar
["96","ar"]
...

Value is a document or the query option include_docs is true

include_docs=true

{"total_rows":4,"offset":0,"rows":[
{"id":"test1","key":"test1","value":null,"doc":{"_id":"test1","_rev":"2-4099492018","a":[1,2,3],"b":"test"}},

Value contains the document (or at least _id):

{"total_rows":4,"offset":0,"rows":[
{"id":"test1","key":null,"value":{"_id":"test1","_rev":"2-4099492018","a":[1,2,3],"b":"test"}},
CouchDbClient client = new DefaultCouchDbClient();
Database database = client.getDatabase(DATABASE);

ViewResult byCountry = database.fetchView(ViewQuery.builder("test/t1").includeDocs(true).build());
for (ViewResultRow row : byCountry) {
    System.out.println(row.getKey());
    Document doc = row.getDocument();
}

Value is a JSON object

Map function

function(doc) {
    if(doc.checked) {
        emit(doc._id, {contact: doc.contact, country: doc.country});
    }
}

View output:

{"total_rows":120,"offset":0,"rows":[
{"id":"026413959081ca1efd13ef2343332249","key":"026413959081ca1efd13ef2343332249","value":{"contact":"12345-98765","country":{"name":"Germany","code":"de","calling_code":"49","export":true}}},

CouchDbClient client = new DefaultCouchDbClient();
Database database = client.getDatabase("dropzones");

ViewResult byCountry = database.fetchView(ViewQuery.builder("admin/checked_dropzones_test").build());
for (ViewResultRow row : byCountry) {
    System.out.println(row.getKey());
    JSONObject obj = row.getValueAsObject();
}
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Anonymous replies:

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account. You can also Sign Up for a new account.