我试图将MongoDB集合中的文档直接读取到自定义Java对象中,而不是默认的Document类中
我在Maven项目中使用mongodb-driver-reactivestreams v1.11.0和Java OpenJDK 11。
我找到了MongoCollection.withDocumentClass(Class)方法,但出现了这个异常:
public class CustomData extends Document {
private static final long serialVersionUID = 783038647854634579L;
@Override
public void putAll(Map<? extends String, ? extends Object> m) {
super.putAll(m);
}
public LocalDateTime getDate() {
Date date = super.getDate("date");
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
}
CustomData.java
MongoDatabase database = MongoDBConnector.getClient().getDatabase(MongoConstants.DATABASE);
MongoCollection<CustomData> collection = database.getCollection(MongoConstants.COLLECTION).withDocumentClass(CustomData.class);
FindPublisher<CustomData> finder = collection.find(Filters.exists(DocumentField.DATE.toString()));
ObservableSubscriber<CustomData> sub = new ObservableSubscriber<>(finder);
return sub.get(DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
这是查找方法:
List<CustomData> customData = manager.getData(); //query
customData.get(0); // <- here i have the exception
我正在用JUnit进行测试,但是当我尝试读取读取的数据时,会收到异常。
airflow initdb