2014-09-30 64 views
0

我必須爲Mongo DB編寫DAO層。如何更新MongoDB的值?

我發現這並不容易。

我的實現很簡單:

刪除的文件具有相同的鍵=>並再次保存

更新它怎麼可能這樣做的元素的列表?

例如JSON表示是下一:

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

這裏是我的代碼:

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

舒爾應該對這個結果有更好的方法。

我無法在mongo文檔中找到smt來獲得此結果。

如何使用Mongo工具實現相同的效果。

回答

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是處理這些類型的問題的選擇。