h2. Value is an Array
Map function:
{code}
function(doc) {
if(doc.country_id){
emit(doc.country.code, [doc.country_id, doc.country.code]);
}
}
{code}
The following CouchDB view result:
{code}
{"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"]},
{code}
can be queried using the {{fetchView}} method:
{code:java}
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);
}
{code}
prints out:
{code}
ae
["4","ae"]
ar
["96","ar"]
ar
["96","ar"]
...
{code}
h2. Value is a document or the query option {{include_docs}} is true
{{include_docs=true}}
{code}
{"total_rows":4,"offset":0,"rows":[
{"id":"test1","key":"test1","value":null,"doc":{"_id":"test1","_rev":"2-4099492018","a":[1,2,3],"b":"test"}},
{code}
Value contains the document (or at least {{\_id}}):
{code}
{"total_rows":4,"offset":0,"rows":[
{"id":"test1","key":null,"value":{"_id":"test1","_rev":"2-4099492018","a":[1,2,3],"b":"test"}},
{code}
{code:java}
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();
}
{code}
h2. Value is a JSON object
Map function
{code}
function(doc) {
if(doc.checked) {
emit(doc._id, {contact: doc.contact, country: doc.country});
}
}
{code}
View output:
{code}
{"total_rows":120,"offset":0,"rows":[
{"id":"026413959081ca1efd13ef2343332249","key":"026413959081ca1efd13ef2343332249","value":{"contact":"12345-98765","country":{"name":"Germany","code":"de","calling_code":"49","export":true}}},
{code}
{code:java}
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();
}
{code}
Map function:
{code}
function(doc) {
if(doc.country_id){
emit(doc.country.code, [doc.country_id, doc.country.code]);
}
}
{code}
The following CouchDB view result:
{code}
{"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"]},
{code}
can be queried using the {{fetchView}} method:
{code:java}
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);
}
{code}
prints out:
{code}
ae
["4","ae"]
ar
["96","ar"]
ar
["96","ar"]
...
{code}
h2. Value is a document or the query option {{include_docs}} is true
{{include_docs=true}}
{code}
{"total_rows":4,"offset":0,"rows":[
{"id":"test1","key":"test1","value":null,"doc":{"_id":"test1","_rev":"2-4099492018","a":[1,2,3],"b":"test"}},
{code}
Value contains the document (or at least {{\_id}}):
{code}
{"total_rows":4,"offset":0,"rows":[
{"id":"test1","key":null,"value":{"_id":"test1","_rev":"2-4099492018","a":[1,2,3],"b":"test"}},
{code}
{code:java}
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();
}
{code}
h2. Value is a JSON object
Map function
{code}
function(doc) {
if(doc.checked) {
emit(doc._id, {contact: doc.contact, country: doc.country});
}
}
{code}
View output:
{code}
{"total_rows":120,"offset":0,"rows":[
{"id":"026413959081ca1efd13ef2343332249","key":"026413959081ca1efd13ef2343332249","value":{"contact":"12345-98765","country":{"name":"Germany","code":"de","calling_code":"49","export":true}}},
{code}
{code:java}
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();
}
{code}