LocalDate not being inserted when using Java driver 4.3.2

My libraries are:

implementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.3.2'
implementation group: 'org.mongodb', name: 'bson', version: '4.3.2'

I have Java objects like:

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;
	}
}

My insertion code is:

MongoDatabase db = MongoDBConnector.getInstance().getDatabase(dbName);
MongoCollection collection = db.getCollection(collectionName, documentClass);
collection.insertMany(documents);

When I am inserting this object into MongoDB, the LocalDates and LocalDateTimes are not being inserted. Previously, these were java.util.Dates. At that time, they were being inserted. After shifting to the new old, they are not. How can I insert them?

1 Like