在MongoDB中插入包含集合的对象

时间:2012-02-22 12:21:57

标签: java mongodb

我是MongoDB / JSON的新手,所以这可能很简单,但我找不到满意的答案。

假设我在下面定义了两个类(实际上要复杂得多):

public class Instrument {
    public String name;
    public List<Identifier> identifiers;
}

public class Identifier {
    public String type;
    public String value;
}

因此,一个仪器可以有多个标识符。 现在我有一个List<Instrument>我希望存储在名为“乐器”的Mongo集合中 到目前为止,我找到的唯一方法是通过逐个插入字段来手动创建每个文档(请参阅下面完整工作示例中的getDocFromInstrument方法)。这非常麻烦且容易出错+与底层类完全结合。

有更好的方法吗?
由于我需要在某个阶段获取信息,因此欢迎任何关于如何“自动”从数据库中重新创建对象的想法。

有关信息,以下代码的输出为:

{ "_id" : { "$oid" : "4f44db111d8bc98c289b5d82"} , "name" : "inst1" , "identifiers" : [ { "type" : "type1" , "value" : "inst1_type1"} , { "type" : "type2" , "value" : "inst1_type2"}]}
{ "_id" : { "$oid" : "4f44db111d8bc98c289b5d83"} , "name" : "inst2" , "identifiers" : [ { "type" : "type1" , "value" : "inst2_type1"} , { "type" : "type2" , "value" : "inst2_type2"}]}

完整代码:

public class TestMongo {

    private final static String IP = "192.168.3.12";
    private final static String DB_NAME = "test";
    private final static int DEFAULT_PORT = 27017;

    public static void main(String[] args) {
        DB db = null;
        try {
            db = new Mongo(IP, DEFAULT_PORT).getDB(DB_NAME);
            insertSomething(db);
            printContent(db);
            cleanDb(db);
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (db != null) {
                db.getMongo().close();
            }
        }
    }

    private static void insertSomething(DB db) {
        Identifier idInst1_1 = new Identifier("type1", "inst1_type1");
        Identifier idInst1_2 = new Identifier("type2", "inst1_type2");
        Identifier idInst2_1 = new Identifier("type1", "inst2_type1");
        Identifier idInst2_2 = new Identifier("type2", "inst2_type2");

        Instrument inst1 = new Instrument("inst1", Arrays.asList(idInst1_1, idInst1_2));
        Instrument inst2 = new Instrument("inst2", Arrays.asList(idInst2_1, idInst2_2));

        BasicDBObject doc1 = getDocFromInstrument(inst1);
        BasicDBObject doc2 = getDocFromInstrument(inst2);

        DBCollection instrumentsCollection = db.getCollection("instruments");
        instrumentsCollection.insert(doc1);
        instrumentsCollection.insert(doc2);
    }

    private static void printContent(DB db) {
        DBCollection instrumentsCollection = db.getCollection("instruments");
        DBCursor cur = instrumentsCollection.find();

        while(cur.hasNext()) {
            System.out.println(cur.next());
        }
    }

    private static void cleanDb(DB db) {
        db.dropDatabase();
    }

    private static BasicDBObject getDocFromInstrument(Instrument instrument) {
    BasicDBObject instrumentDoc = new BasicDBObject();
    instrumentDoc.put("name", instrument.name);
    List<BasicDBObject> identifiers = new ArrayList<>();
    for (Identifier identifier : instrument.identifiers) {
        BasicDBObject identifierDoc = new BasicDBObject();
        identifierDoc.put("type", identifier.type);
        identifierDoc.put("value", identifier.value);
        identifiers.add(identifierDoc);
    }
    instrumentDoc.put("identifiers", identifiers);
        return instrumentDoc;
    }

    static class Instrument {
        public String name;
        public List<Identifier> identifiers;

        public Instrument(String name, List<Identifier> ids) {
            this.name = name;
            this.identifiers = ids;
        }

    }

    static class Identifier {
        public String type = "";
        public String value = "";

        public Identifier(String type, String values) {
            this.type = type;
            this.value = values;
        }

    }
}

4 个答案:

答案 0 :(得分:3)

看看Morphia,它是MongoDB的Java ORM:

Morphia - Java ORM to/from MongoDB

我将它用于我的Java代码,我的类与你的类非常相似。我没有遇到任何在其他对象中嵌套对象列表的麻烦。 Morphia使用反射将您的类转换为JSON,因此它将为您处理大量代码(例如您的getDocFromInstrument函数)。希望有所帮助。

答案 1 :(得分:3)

你可以尝试几种ORM工具。但Morphia是其中最稳定的工具。

答案 2 :(得分:1)

https://github.com/impetus-opensource/Kundera/wiki/Kundera-Mongo-performance

这是一个汇编的性能比较,包括昆德拉,Spring Data,Morphia和Native API。

这应该让你知道哪一个更好。 Kundera正在提供客户端扩展框架(一种处理其他mongodb要求的方法)。

答案 3 :(得分:0)

mongo的网站上有a list of Java POJO mappers