java - 使用 MongoDB Java 驱动程序时未插入 LocalDate
问题描述
我的图书馆是:
implementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.3.2'
implementation group: 'org.mongodb', name: 'bson', version: '4.3.2'
我有 Java 对象,例如:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;
import org.bson.codecs.pojo.annotations.BsonProperty;
import org.bson.types.ObjectId;
public class TestFile {
protected ObjectId id;
@BsonProperty(value = "report_date")
public LocalDate reportDate;
@BsonProperty(value = "asset_title")
public String assetTitle;
@BsonProperty(value = "asset_type")
public String assetType;
@BsonProperty(value = "revenue")
public Double revenue;
@BsonProperty(value = "modified_datetime")
private LocalDateTime modifiedDateTime;
public ObjectId getId() {
return id;
}
public TestFile setId(ObjectId id) {
this.id = id;
return this;
}
public LocalDate getReportDate() {
return reportDate;
}
public TestFile setReportDate(LocalDate reportDate) {
this.reportDate = reportDate;
return this;
}
public String getAssetTitle() {
return assetTitle;
}
public TestFile setAssetTitle(String assetTitle) {
this.assetTitle = assetTitle;
return this;
}
public String getAssetType() {
return assetType;
}
public TestFile setAssetType(String assetType) {
this.assetType = assetType;
return this;
}
public Double getRevenue() {
return revenue;
}
public TestFile setRevenue(Double revenue) {
this.revenue = revenue;
return this;
}
public LocalDateTime getModifiedDateTime() {
return modifiedDateTime;
}
public TestFile setModifiedDateTime(LocalDateTime modifiedDateTime) {
this.modifiedDateTime = modifiedDateTime;
return this;
}
}
我的插入代码是:
MongoDatabase db = MongoDBConnector.getInstance().getDatabase(dbName);
MongoCollection collection = db.getCollection(collectionName, documentClass);
collection.insertMany(documents);
当我将此对象插入 MongoDB 时,没有插入LocalDate
s 和s。LocalDateTime
以前,这些是java.util.Date
s。那时,它们正在被插入。换到新旧后,他们就没有了。我怎样才能插入它们?
解决方案
推荐阅读
- mysql - 触发器未在 MySQL 中触发
- python - 一维数组和二维数组之间的numpy元素明智乘法背后的逻辑
- python - 我如何知道哪个基类将特定属性添加到子类对象
- javascript - Chrome 41 解析 JSON 对象令牌错误
- php - 如何使用 fpdf 在所有页面中设置表头?
- javascript - JSplumb 连接到多个端点
- azure - 如何在 Azure 中找到 billingAccountId
- c++ - AddressSanitizer 拦截器_via_fun
- python - 从二维数据框制作单列数据框
- java - 在 JTree 的 FileNode 中设置颜色