2016-12-19 1 views
0

안녕하세요. Mongo Pojo 목록을 사용하고 있습니다. upsert를 사용하고 싶습니다. 내 pojo를 사용하는 방법을 모르겠습니다.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이며,이 코드는 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); 
    } 

답변

0

몽고 자바 드라이버는 복합 유형에 대한 지원이없는 당신의 POJO를 봄 몽고 DB를 주석이있는 것처럼 보이는 내 AgentDataStorage입니다.

여기에 두 개의 옵션이 있습니다. 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

관련 문제