Couchbase Java API and javascript view not returning value for a specific Key

469 Views Asked by At

I am using couchbase API in java

View view = client.getView("dev_1", "view1");
    Query query = new Query();
    query.setIncludeDocs(true);
    query.setKey(this.Key);
    ViewResponse res=client.query(view, query);
    for(ViewRow row: res)   
    {
          // Print out some infos about the document
        a=a+" "+row.getKey()+" : "+row.getValue()+"<br/>";

    }

    return a;

and the java script view in couchbase

function (doc,meta) {
  emit(meta.id,doc);
}

So, when I remove the statement query.setkey(this.Key) it works returns me all the tables, what am I missing here .. How can I change the function to refect only the table name mentioned in the key

1

There are 1 best solutions below

0
ColinWa On

Change the map function like this:

      function (doc,meta) {
           emit(doc.table,null);
      }

it is good practice not to emit the entire document like:

      emit(doc.table, doc)

NB: This is surprisingly important:

i have tried using setKey("key") so many times from Java projects and setting the key using CouchBase Console 3.0.1's Filter Result dialog, but nothing get returned.

One day, i used setInclusiveEnd and it worked. i checked the setInclusiveEnd checkbox in CouchBase Console 3.0.1's Filter Result dialog and i got json output.

    query.setKey("whatEverKey");
    query.setInclusiveEnd(true);

i hope this will be helpful to others having the same issue. if anyone finds another way out, please feel free to add a comment about it.

i don't know why their documentation does not specify this.

EXTRA

If your json is derived from an entity class in a Java Project, make sure to include an if statement to test the json field for the entity class name to enclose you emit statement. This will avoid the key being emitted as null:

      if(doc._class == "path.to.Entity") {
          emit(doc.table, null);
      }