【问题标题】:Inserting and fetching the Large JSON document in Couchbase?在 Couchbase 中插入和获取大型 JSON 文档?
【发布时间】:2016-06-29 18:51:53
【问题描述】:

我正在尝试在 Couchbase 中插入大型 JSON 文档。我已插入如下文档。

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);
bucket.upsert(JSONDocument.create("exam", jsonObj));

像上面那样插入文档后,我想在获取时检索单个嵌套节点(例如:问题)。

我有以下疑问: 1)我可以使用传统方法插入文档吗: 字符串查询=“插入默认(键,值)值(jsondoc)”; statement.executeUpdateQuery(查询); 或者我是否需要在每个 JSONObject 中单独插入上述嵌套节点才能正确获取嵌套节点?

2)如何使用 N1QLQueryResult 来获取每个 json 文档作为一行只获取所需的 json 信息

【问题讨论】:

    标签: json couchbase couchbase-java-api


    【解决方案1】:

    您使用的是哪个版本的 Couchbase? 4.5 版中有一个子文档 API,用于对部分 JSON 文档进行操作:

    http://developer.couchbase.com/documentation/server/4.5-dp/sub-doc-api.html

    对于第二个问题,您可能需要查看 UNNEST N1QL 运算符。

    【讨论】:

    • 我正在使用 Couchbase 4.0
    • 您好@beamatronic,我只将整个文档插入到jsonObject 中。那么是否有可能获取json文档的部分?
    • 正如@beamatronic 所说:是的,您可以使用UPSERT 或INSERT。您需要为 KEY 和 VALUE 指定表达式。请参阅文档中的语法。对于检索,UNNEST 会将每个问题作为单独的行提供给您。或者您可以指定一个数组下标来按位置检索问题。
    • 您好@geraldss,感谢您的回复。我试图排列下标。但我无法得到它。如果你能给我一些例子,那就太好了。同时,我将使用 UNNEST 获取它。
    • LMK 如果 UNNEST 适合您。数组下标表示exams.exam1.year.math.questions[0]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多