1
votes

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?

1

1 Answers

1
votes

Why dont you create a simple java pojo and define it as a @Document. Then use CrudRepo to save and retrieve documents from couchbase.

this sample might help you: https://github.com/maverickmicky/spring-couchbase-cache