2014-09-30 9 views
0

Mongo DB 용 DAO 레이어를 작성해야합니다.Mongo DB에서 값을 업데이트하는 방법은 무엇입니까?

나는 그렇게 쉬운 일이 아니라는 것을 알았다.

가 동일한 키 =으로 문서를 삭제> 및 요소의 목록을 위해 그것을 할 방법이 가능 다시

을 업데이트 저장 :

내 구현은 간단하다?

예를 들어 JSON 표현은 다음과 같습니다 슈어 들어

@Override 
public void update(MODEL model) { 
    try { 
     Field keyField1 = getKeyField(model); 
     String fieldValue = getKeyFieldValue(keyField1, model); 
     BasicDBObject query = createQuery(keyField1.getName(), fieldValue); 
     DBCursor cursor = dbCollection.find(query); 
     if (cursor.hasNext()) { 
      DBObject dbObject = getDbObject(model); 
      dbCollection.update(query, dbObject); 
     } else { 
      throw new RuntimeException(String.format("Data status %s isn't presented at %s with value %s", keyField1.getName(), dbCollection.getFullName(), fieldValue)); 
     } 
    } catch (IOException e) { 
     Log.getInstance().log(e.getCause()); 
    } 
} 

private Field getKeyField(MODEL model) { 
    Field keyField = null; 
    for (Field field : model.getClass().getDeclaredFields()) { 
     if (field.isAnnotationPresent(KeyField.class)) { 
      keyField = field; 
     } 
    } 
    if (keyField == null) { 
     throw new RuntimeException(String.format("Couldn't find KeyField annotation at class '%s'", model.getClass().getName())); 
    } 
    return keyField; 
} 

private String getKeyFieldValue(Field keyField, Object model) { 
    String result = null; 
    try { 
     if(keyField.isAnnotationPresent(KeyField.class)) { 
      keyField.setAccessible(true); 
      result = keyField.get(model).toString(); 
     } 
     if(result == null || result.equals("")) { 
      throw new NullPointerException(); 
     } 
    } catch(NullPointerException e) { 
     throw new RuntimeException("KeyField property is empty"); 
    }catch (Exception e) { 
     throw new RuntimeException(String.format("Couldn't find KeyField annotation at class '%s'", model.getClass().getName())); 
    } 
    return result; 
} 

private BasicDBObject createQuery(String key, String value) { 
    BasicDBObject query = new BasicDBObject(); 
    query.put(key, value); 
    return query; 
} 

이 결과를 훨씬 더 좋은 방법이 존재한다 :

"airItinerary" : { 
    "originDestinationOptions" : { 
     "originDestinationOption" : [ { 
     "flightSegment" : [ { 
      "departureAirport" : { 
      "locationCode" : "DUB", 
      "codeContext" : "IATA" 
      }, 
      "arrivalAirport" : { 
      "locationCode" : "CDG", 
      "codeContext" : "IATA" 
      }, 

여기 내 코드입니다.

이 결과를 얻으려면 mongo doc에서 smt를 찾을 수 없습니다.

몽고 도구로 동일한 효과를 얻는 방법.

답변

0

직접 업데이트 할 수 있습니다. 첫 번째 단계로 찾을 필요가없고 업데이트 찾기 사이에 불일치가 발생할 수 있습니다.

@Override 
public void update(MODEL model) { 
    try { 
     Field keyField1 = getKeyField(model); 
     String fieldValue = getKeyFieldValue(keyField1, model); 
     BasicDBObject query = createQuery(keyField1.getName(), fieldValue); 

     DBObject dbObject = getDbObject(model); 
     dbCollection.update(query, dbObject); 

    /* DBCursor cursor = dbCollection.find(query); 
     if (cursor.hasNext()) { 
      DBObject dbObject = getDbObject(model); 
      dbCollection.update(query, dbObject); 
     } else { 
      throw new RuntimeException(String.format("Data status %s isn't presented at %s with value %s", keyField1.getName(), dbCollection.getFullName(), fieldValue)); 
     } 
    */ 
    } catch (IOException e) { 
     Log.getInstance().log(e.getCause()); 
    } 
} 

private Field getKeyField(MODEL model) { 
    Field keyField = null; 
    for (Field field : model.getClass().getDeclaredFields()) { 
     if (field.isAnnotationPresent(KeyField.class)) { 
      keyField = field; 
      break; // add a break to jump out quickly. 
     } 
    } 
    if (keyField == null) { 
     throw new RuntimeException(String.format("Couldn't find KeyField annotation at class '%s'", model.getClass().getName())); 
    } 
    return keyField; 
} 

또한 spring-data-mongodb은 이러한 종류의 문제를 처리하기위한 선택입니다.

관련 문제