Working with CRUD operations in Couchbase

I am novice User to Couchbase, I am trying to insert the documents into the default bucket like below.
I found the following 2 ways to insert the json documents in the bucket:

  1. Inserting by preparing JsonDocument and upsert into the bucket

    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append("{'phone':{'y':{'phonePropertyList':{'dskFlag':'false','serialId':1000,'inputTray':{'LIST': {'e':[{'inTray':{'id':'1','name':'BypassTray','amount': {'unit':'sheets','state':'empty','typical':'0','capacity':'100'}");
    String LDATA = strBuilder.toString();   
    Cluster cluster = CouchbaseCluster.create("localhost");
    

    Bucket bucket = cluster.openBucket(“default”);
    JsonObject deviceinfoObj = JsonObject.create().put(“phoneinfo”, LDATA);
    bucket.upsert(JsonDocument.create(“phone”, deviceinfoObj));

  2. Or by using like direct query like SQL
    String query = “upsert into default(KEY, VALUE) values(LDATA)”

I am not able to find how to execute the above query like noram sql statement
Example:
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(query);

How to use N1QLQuery to insert the json document in the Couchbase Bucket.

I found the two ways to fetching the document.

i) Getting the document directly by using document Id
bucket.get(“phone”).content().get(“phoneinfo”)

ii) Getting the documents by using N!QueryResultSet
N1qlQueryResult result = bucket.query(N1qlQuery.simple(“select * from default;”));

            for (N1qlQueryRow row : result) { 
           System.out.println(row);
        }

I got confused with the different approaches for inserting and fetching the documents from/to bucket in the Couchbase.
If I insert the documents by using 1st approach I need to prepare JsonObject with some key and value as a entire jsondocument.

So I think I better to insert the document using the second approach, So I will be able to fetch the documents using N1QLResultSet(2nd approach). but by using first approach I need to get the number of documents in the bucket and and then only I can loop through all the documents

Queries:

  1. How to get the selective nested nodes from the document
  2. In json document, Should I need to split key-value for each node ant then put in the JSONObject to prepare JSONDocument?
  3. How to create a view for the bucket to do fast retrieval?

Hi Suri, Couchbase provides different ways to access data. You can use KV interface or N1QL, or combination of both based on your needs. I din’t quite get what is your question w.r.t the two approaches you mentioned.

  1. You can get specific attributes (or nested elements) using both KV (http://developer.couchbase.com/documentation/server/4.5/sdk/subdocument-operations.html) and N1QL (specify projection list http://developer.couchbase.com/documentation/server/4.5/sdk/n1ql-query.html)

  2. by node, I guess you are referring to the attributes in the document. To create the json document, you need the document-key, and document-value (which is the JSON document with all its nested attributes etc). http://developer.couchbase.com/documentation/server/4.5/sdk/java/document-operations.html

  3. Views is one way of processing documents. Couchbase also has Global secondary Indexes which provide faster retrieval through N1QL. And, if you want to retrieve documents by the document-key, then the KV interface may work better. Right choice may depends on various things like, number of documents you fetch and the kind of processing to be done on the documents etc., You can find more info at http://developer.couchbase.com/documentation/server/4.5/data-access/data-access-intro.html