我是MongoDB /
JSON的新手,所以这可能很简单,但我找不到满意的答案.
假设我在下面定义了2个类(实际上要复杂得多):
public class Instrument {
public String name;
public List 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 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 identifiers;
public Instrument(String name, List 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;
}
}
}