如何在Bulk Upsert中使用Mongo Pojos

时间:2016-12-19 08:25:26

标签: spring mongodb spring-data

您好我有一个Mongo Pojo列表我想使用upsert。我不知道如何使用我的pojo。

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

1 个答案:

答案 0 :(得分:0)

Mongo Java Driver不支持复杂类型,看起来你的pojos是用spring mongo db annotation注释的。

您在这里有几个选项MongoTemplateMongoRepository

MongoTemplate:http://docs.spring.io/spring-data/data-mongo/docs/1.10.0.M1/reference/html/#mongo-template

MongoRepository:http://docs.spring.io/spring-data/data-mongo/docs/1.10.0.M1/reference/html/#mongo.repositories

相关问题