How to fetch the required json node from the Couchbase bucket document?

I am trying to insert the Large JSON document in the Couchbase. I have inserted the document like below.

Bucket bucket = cluster.openBucket("default");
String jsondoc = "{{
"exams": {
    "exam1": {
        "year": {
            "math": {
                "questions": [
                    {
                        "question_text": "first question",
                        "options": [
                            "option1",
                            "option2",
                            "option3",
                            "option4",
                            "option5"
                        ],
                        "answer": 1,
                        "explaination": "explain the answer"
                    },
                    {
                         "question_text": "second question",
                        "options": [
                            "option1",
                            "option2",
                            "option3",
                            "option4",
                            "option5"
                        ],
                        "answer": 1,
                        "explaination": "explain the answer"
                    },
                    {
                        "question_text": "third question",
                        "options": [
                            "option1",
                            "option2",
                            "option3",
                            "option4",
                            "option5"
                        ],
                        "answer": 1,
                        "explaination": "explain the answer"
                    }
                ]
            },
            "english": {same structure as above}
        },
        "1961": {}
    },
    "exam2": {},
    "exam3": {},
    "exam4": {}
}
}}";

JSONObject jsonObj = new JSONObject();
jsonObj.put("examinfo", jsondoc); // Entire JSON Document(String jsondoc) is keeping as value for the JSON Object.
bucket.upsert(JSONDocument.create("exam", jsonObj));

After Inserting the document like above, I want to retrieve individual nested nodes(ex: questions) while fetching.

How to get Particular Node from the above inserted document. I have used array subscripting(exams.exam1.year.math.questions) but It gives empty as a result.

Note: I am using Couchbase 4.0 community Edition, So I can not use subdoc feature intriduced in 4.5.

Could some body Please tell me what I am doing wrong here or Is my insertion is not proper?

Hi,

let’s first start with your ingestion here - what are you exactly doing with JSONObject and friends? From which imports are they? If you want to store raw JSON I’d suggest you use:

bucket.upsert(RawJsonDocument.create("exam", jsondoc)); directly without other encoding/decoding.

Once the document is stored as proper JSON on the server you have two choices to receive a subpath: via N1QL or via subdoc if you are using 4.5. Since you are using 4.0 the only way to get a subpath directly is N1QL. (note that you can always load the full doc via KV and then grab the subpath).

You can use N1QL like

SELECT exams.exam1.year.math.questions FROM `bucketname` USE KEYS ["exam"];