0
嗨,我有一個Mongo Pojo的列表,我想使用upsert。我不知道如何使用我的pojo。如何在散裝Upsert中使用Mongo Pojos
@Test
public void testAgentDataStorage() {
AgentDataStorage a = new AgentDataStorage(124l);
DBCollection collection = mongoTemplate.getDb().getCollection("agent_data_storage");
BulkWriteOperation bulkWriteOperation = collection.initializeUnorderedBulkOperation();
BulkWriteRequestBuilder bulkWriteRequestBuilder = bulkWriteOperation.find((DBObject) a);
BulkUpdateRequestBuilder updateReq = bulkWriteRequestBuilder.upsert();
a.getDataPoints().put("TOTAL_INCENTIVE_EARNINGS" , 13);
updateReq.replaceOne((DBObject) a);
BulkWriteResult result = bulkWriteOperation.execute();
}
這裏AgentDataStorage是我的POJO和這個代碼給錯誤 AgentDataStorage不能轉換到com.mongodb.DBObject。 下面是我AgentDataStorage
@CompoundIndex(name = "account_date_idx", def = "{'account' : 1, 'date' : 1}", unique = true)
@Document(collection = "agent_data_storage")
public class AgentDataStorage extends MongoKeyedEntity<String> implements Serializable {
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Field
private Long account;
@Field()
private String date;
@Field
private Map<String, Integer> dataPoints = new HashMap<>();
public AgentDataStorage(Long account) {
this.account = account;
this.date = dateFormat.format(new Date());
for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
this.dataPoints.put(dataPoint.toString(), 0);
}
}
public AgentDataStorage(String account) {
this.account = Long.valueOf(account);
for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
this.dataPoints.put(dataPoint.toString(), 0);
}
}
public AgentDataStorage(Long account, Date date) {
this.account = account;
this.date = dateFormat.format(date);
for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
this.dataPoints.put(dataPoint.toString(), 0);
}
}
public AgentDataStorage(Long account, Date date, Map<String, Integer> dataPoints) {
this.account = account;
this.date = dateFormat.format(date);
this.dataPoints = dataPoints;
}
public AgentDataStorage(String account, Date date) {
this.account = Long.valueOf(account);
this.date = dateFormat.format(date);
for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
this.dataPoints.put(dataPoint.toString(), 0);
}
}
public AgentDataStorage(String account, String date) {
this.account = Long.valueOf(account);
this.date = date;
for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
this.dataPoints.put(dataPoint.toString(), 0);
}
}
public Long getAccount() {
return account;
}
public void setAccount(Long account) {
this.account = account;
}
public Date getDate() throws ParseException {
return dateFormat.parse(this.date);
}
public void setDate(Date date) {
this.date = dateFormat.format(date);
}
public Map<String, Integer> getDataPoints() {
return dataPoints;
}
public void setDataPoints(Map<String, Integer> dataPoints) {
this.dataPoints = dataPoints;
}
public void updateDataPoint(AgentDataPoints agentDataPoints, Integer value) {
this.dataPoints.put(String.valueOf(agentDataPoints), value);
}