title: How to update value at Mongo DB?
body: <p>I have to write DAO layer for Mongo DB.</p>
  
  <p>I found that this isn't so easy.</p>
  
  <p>My implementation is straightforward: </p>
  
  <blockquote>
    <p>delete document with the same key => and save updated it again</p>
  </blockquote>
  
  <p>How possible to do it for List of elements?</p>
  
  <p>For example JSON representation is next:</p>
  
  <pre><code>  "airItinerary" : {
      "originDestinationOptions" : {
        "originDestinationOption" : [ {
          "flightSegment" : [ {
            "departureAirport" : {
              "locationCode" : "DUB",
              "codeContext" : "IATA"
            },
            "arrivalAirport" : {
              "locationCode" : "CDG",
              "codeContext" : "IATA"
            },
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>@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;
  }
  </code></pre>
  
  <p>For shure should exist much better way for this result.</p>
  
  <p>I can't find smt at mongo doc for achieving this result.</p>
  
  <p><strong><em>How to achive the same effect with Mongo tools.</em></strong></p>