我如何从mongodb生成json架构导致java?

时间:2014-12-17 10:03:28

标签: java mongodb jsonschema

我有一个简单的mongodb查询,需要结果的json模式。

    MongoClientURI uri = new MongoClientURI("mongodb://localhost:27017");
    String database = "test_m0001";

    MongoClient mongo = new MongoClient(uri);
    DB db = mongo.getDB(database);
    DBCollection coll = db.getCollection(collectionName);

    try (DBCursor cursor = coll.find().limit(100)) {
        while (cursor.hasNext()) {
            DBObject document = cursor.next();
            System.out.println(document);
            // --> generate json schema for document
        }
    }
    // --> merge json schema for all documents from query

在java中生成json架构的方法是什么?我查看了Jackson JSON Schema Modul,但我找不到任何答案。我没有模型类,只有mongodb集合中的未知结构化文档。

1 个答案:

答案 0 :(得分:0)

使用以下代码。它可能会起作用

MongoClientURI uri = new MongoClientURI("mongodb://localhost:27017");
    String database = "test_m0001";
    String json="";
    MongoClient mongo = new MongoClient(uri);
    DB db = mongo.getDB(database);
    DBCollection coll = db.getCollection(collectionName);

    try (DBCursor cursor = coll.find().limit(100)) {
        while (cursor.hasNext()) {
            DBObject document = cursor.next();
             StringWriter out = new StringWriter();
             JSONValue.writeJSONString(document.toMap(), out);
             json=out.toString();
             System.out.println(json);
        }
    }

它会给出如下的架构:

{
    "_id": 5458 bee9d4ae9599b3ef2458,
    "author": "skc",
    "body": "My Programme",
    "comments": [{
        "author": "SCac",
        "body": "acsac",
        "email": "ascac"
    }, {
        "author": "Sitakant",
        "body": "ZScassc",
        "email": "Chaudhury"
    }],
    "date": Tue Nov 04 17: 26: 25 IST 2014,
    "permalink": "advcjnknl",
    "tags": ["asdcac"],
    "title": "advcjnknl"
} {
    "_id": 5458e a31157fe93550017831,
    "author": "SEEuewe",
    "title": "SkBaSmGaTuELeYUxRUIbFptrcyHKSJ",
    "body": "BGfaVSsHqolIfiwTpPOliWBHjkWlYa",
    "permalink": "skbasmgatueleyuxruibfptrcyhksj",
    "tags": ["tlpOe", "FbcGj", "pQlDf"],
    "date": Tue Nov 04 20: 31: 05 IST 2014,
    "comments": []
} {
    "_id": 5458e a31157fe93550017832,
    "author": "SEEuewe",
    "title": "DwCyowWKVxXUjpZUcFlHrhHpUzoKTh",
    "body": "zwkIAboiGhOejkHjdFuUAufcbyJapf",
    "permalink": "dwcyowwkvxxujpzucflhrhhpuzokth",
    "tags": ["YabrH", "qNUTy", "TmFwV"],
    "date": Tue Nov 04 20: 31: 05 IST 2014,
    "comments": []
} {
    "_id": 5458e a47157fe93550017833,
    "author": "CoPSPVk",
    "title": "NodErjzZvnRSzzWazYWLxGFslYcZDf",
    "body": "AfFGNtskOhVCzjvhQlGkJxGnYLrlVk",
    "permalink": "noderjzzvnrszzwazywlxgfslyczdf",
    "tags": ["IuPiv", "OubeB", "RnvWj"],
    "date": Tue Nov 04 20: 31: 27 IST 2014,
    "comments": []
} {
    "_id": 5458e a47157fe93550017834,
    "author": "CoPSPVk",
    "title": "oMKsjnkSXEtRlUcJEGBJgJbhkHcEzi",
    "body": "IvvcTOxCRpaJAzKoYisaCDKbeZnwsl",
    "permalink": "omksjnksxetrlucjegbjgjbhkhcezi",
    "tags": ["lqQhV", "mabGI", "xtPex"],
    "date": Tue Nov 04 20: 31: 27 IST 2014,
    "comments": []
} {
    "_id": 5458e a9d157fe93550017835,
    "author": "skc",
    "title": "Sitakant's Post",
    "body": "Here is the first Post of Sitakant",
    "permalink": "sitakants_post",
    "tags": ["JavaMongo"],
    "date": Tue Nov 04 20: 32: 53 IST 2014,
    "comments": []
} {
    "_id": 5458e b3e157f67f5ba156fa5,
    "author": "fNrSxIY",
    "title": "ZgjCajXasUObSUxRJvzpTYMtLiCjgH",
    "body": "fjzpLxOFRqVNDHmLwUePLMgSKcXMNA",
    "permalink": "zgjcajxasuobsuxrjvzptymtlicjgh",
    "tags": ["ftXfi", "sdTmw", "JhAtS"],
    "date": Tue Nov 04 20: 35: 34 IST 2014,
    "comments": []
} {
    "_id": 5458e b3e157f67f5ba156fa6,
    "author": "fNrSxIY",
    "title": "ImvrpnvgcvYEvYDqtGGXHVZONCPHbf",
    "body": "aaxGLzdiEPJVSLvrjsZdcwcgllMyLb",
    "permalink": "imvrpnvgcvyevydqtggxhvzoncphbf",
    "tags": ["FVFqj", "FlNNe", "vLmRo"],
    "date": Tue Nov 04 20: 35: 34 IST 2014,
    "comments": []
} {
    "_id": 5458e b48157f67f5ba156fa7,
    "author": "VAPNMXd",
    "title": "TljGJFvDqFQupouzmNFxPKvMnOJoJx",
    "body": "mucDhfFCNeBhpywYaCZhVSWZcVywoW",
    "permalink": "tljgjfvdqfqupouzmnfxpkvmnojojx",
    "tags": ["TqSdG", "IUfWr", "wPjlc"],
    "date": Tue Nov 04 20: 35: 44 IST 2014,
    "comments": []
} {
    "_id": 5458e b48157f67f5ba156fa8,
    "author": "VAPNMXd",
    "title": "LuqwcEFMHcwhbXnPHYDgtnCFxCPdUk",
    "body": "bKBYAfjJjtegKneCnUNVWUnbWcbhqW",
    "permalink": "luqwcefmhcwhbxnphydgtncfxcpduk",
    "tags": ["rXaFH", "hnbho", "RMQlm"],
    "date": Tue Nov 04 20: 35: 44 IST 2014,
    "comments": []
} {
    "_id": 5458e b59157f67f5ba156faa,
    "author": "TNBFjFx",
    "title": "hfYYvpEtlGOFhicEkXmwFGpPhNoUSP",
    "body": "TrShIejThHmjqrvoGpaEWGpPpEgYbK",
    "permalink": "hfyyvpetlgofhicekxmwfgpphnousp",
    "tags": ["OqTEO", "zYNIS", "vUpNa"],
    "date": Tue Nov 04 20: 36: 01 IST 2014,
    "comments": []
} {
    "_id": 5458e b59157f67f5ba156fa9,
    "author": "TNBFjFx",
    "body": "ldmqzeZBTtANiBlmVHTvqZhAeBLmXU",
    "comments": [{
        "author": "cGgBIQXTcrMG",
        "body": "VinmjjhWjyTJ"
    }],
    "date": Tue Nov 04 20: 36: 01 IST 2014,
    "permalink": "kjlxmnkxzznomshgwldtlvfeflmmkf",
    "tags": ["LMfVJ", "PuUxy", "XZfpL"],
    "title": "kJlXMnkxZzNOMSHgWLdTlvFeflmMKf"
} {
    "_id": 5458e b5f157f67f5ba156fac,
    "author": "ofrjKsJ",
    "title": "nskhDjTuGFHXqTwqnLBBFSYcyStwEM",
    "body": "BmSDDQBbKVyjDmRjFJEVrulSBorMFL",
    "permalink": "nskhdjtugfhxqtwqnlbbfsycystwem",
    "tags": ["kvjic", "PnvyS", "ZVQND"],
    "date": Tue Nov 04 20: 36: 07 IST 2014,
    "comments": []
} {
    "_id": 5458e b5f157f67f5ba156fab,
    "author": "ofrjKsJ",
    "body": "TzybDUtlJzkkBqWwaApplHkamFXkpM",
    "comments": [{
        "author": "QHwtRAJCNjvU",
        "body": "gqQIEmTaQsOs"
    }],
    "date": Tue Nov 04 20: 36: 07 IST 2014,
    "permalink": "abfqlmxkkdxiqpjuflvgqcocpxsxms",
    "tags": ["bzkEn", "IdXyE", "nblih"],
    "title": "abFQlmXkKdxiqPjUFLVgQCOcpxsXms"
}