我是MongoDB的新手,我在将JSON格式的文档存储到我的数据库时遇到了一些麻烦。我为我的应用程序提供了一个测试套件,但由于我的文档格式化(我假设),我无法通过一个特定的测试。 任何帮助将不胜感激。
提前致谢, 顺
这是一直失败的断言:
java.lang.AssertionError:
Expected :{ "log_set" : ["SCHEDULED", "STREAMING", "CANCELED", "FINISHED"] }
Actual :Document{{log_set=[SCHEDULED, STREAMING, CANCELED, FINISHED]}}
以下是我插入数据的代码:
@Override
public void loadData() throws Exception {
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
MongoDatabase database = mongoClient.getDatabase("dst");
MongoCollection<Document> collection = database.getCollection("EventData");
em.getTransaction().begin();
Query query = em.createNamedQuery(Constants.Q_ALLFINISHEDEVENTS);
List<EventImpl> finishedEvents= query.getResultList();
em.getTransaction().commit();
for(int i=0; i<finishedEvents.size(); i++){
Document toInsert = new Document("event_id",finishedEvents.get(i).getId());
toInsert.append(Constants.PROP_EVENTFINISHED,finishedEvents.get(i).getEventStreaming().getEnd().getTime());
toInsert.append(mongoTestData.getDataDescription(finishedEvents.get(i).getId()),mongoTestData.getData(finishedEvents.get(i).getId()));
System.out.println("DATE ENDED: "+finishedEvents.get(i).getEventStreaming().getEnd().getTime());
toInsert.append(Constants.PROP_EVENTFINISHED,finishedEvents.get(i).getEventStreaming().getEnd().getTime());
System.out.println("DOCUMENT TO INSERT:"+toInsert.toString());
System.out.println("DOCUMENT JSON: " +toInsert.toJson());
//collection.insertOne(toInsert);
BasicDBObject dbObject = (BasicDBObject) JSON.parse(toInsert.toJson());
collection.insertOne(new Document(dbObject));
}
}
编辑:添加了测试和测试数据:
这是测试代码:
@Test
public void testMongoDataLoader() throws Exception {
MongoCollection<Document> collection = db.getCollection(Constants.COLL_EVENTDATA);
Map<Long, Document> docs = idMap(collection, d -> d.getLong(Constants.I_EVENT));
List<IEvent> list = daoFactory.getEventDAO().findAll();
assertFalse(list.isEmpty());
assertEquals(list.size(), docs.size());
for (IEvent relObj : list) {
assertTrue(docs.containsKey(relObj.getId()));
assertDataEquals(docs.get(relObj.getId()), relObj);
}
}
protected void assertDataEquals(Document document, IEvent relObj) {
MongoTestData mongoTestData = new MongoTestData();
assertEquals(
document.getLong(Constants.PROP_EVENTFINISHED).longValue(),
relObj.getEventStreaming().getEnd().getTime()
);
assertEquals(
document.get(mongoTestData.getDataDescription(relObj.getId())),
Document.parse(mongoTestData.getData(relObj.getId()))
);
}
这是MongoTestData:
public class MongoTestData {
public static final String DATA_DESC_LOGS = "logs";
public static final String DATA_DESC_MATRIX = "matrix";
public static final String DATA_DESC_BLOCK = "alignment_block";
private ArrayList<String> testData = new ArrayList<>();
private ArrayList<String> testDataDesc = new ArrayList<>();
public MongoTestData() {
String s1 = "{ \"log_set\" : [\"SCHEDULED\", \"STREAMING\", \"CANCELED\", \"FINISHED\"] }";
String s2 = "{ \"matrix_data\" : [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]}";
String s3 = "{ \"alignment_nr\" : 0, \"primary\" : { "
+ " \"chromosome\" : \"chr11\", \"start\" : 3001012, \"end\" : 3001075 }, \"align\" : { "
+ " \"chromosome\" : \"chr13\", \"start\" : 70568380, \"end\" : 70568443 }, \"blastz\" : 3500, "
+ "seq : [\"TCAGCTCATAAATCACCTCCTGCCACAAGCCTGGCCTGGTCCCAGGAGAGTGTCCAGGCTCAGA\", "
+ "\"TCTGTTCATAAACCACCTGCCATGACAAGCCTGGCCTGTTCCCAAGACAATGTCCAGGCTCAGA\"] }";
testData.add(s1);
testData.add(s2);
testData.add(s3);
testDataDesc.add(DATA_DESC_LOGS);
testDataDesc.add(DATA_DESC_MATRIX);
testDataDesc.add(DATA_DESC_BLOCK);
}
public String getData(Long eventId) {
return testData.get(eventId.intValue() % 3);
}
public String getDataDescription(Long eventId) {
return testDataDesc.get(eventId.intValue() % 3);
}
}
答案 0 :(得分:2)
更改插入代码以将数据插入Document
(键值对)而不是原始json字符串。
将for loop
代码更新为以下内容。请注意使用Document.parse
for(int i=0; i<finishedEvents.size(); i++){
Document toInsert = new Document("event_id", finishedEvents.get(i).getId());
toInsert.append(Constants.PROP_EVENTFINISHED, finishedEvents.get(i).getEventStreaming().getEnd().getTime());
toInsert.append(mongoTestData.getDataDescription(finishedEvents.get(i).getId()), Document.parse(mongoTestData.getData(finishedEvents.get(i).getId())));
collection.insertOne(toInsert);
}
答案 1 :(得分:1)
您的测试工具似乎期待JSON,但它正在获取Document实例。由于这两者并不严格相等,因此比较失败。所以我说这可能是您的测试工具或您如何使用它的问题。