如何更新Mongodb的价值?

时间:2014-09-30 10:59:36

标签: java mongodb crud

我必须为Mongo DB编写DAO层。

我发现这并不容易。

我的实施很简单:

  

使用相同的密钥删除文档=>并保存再次更新

如何为元素列表做到这一点?

例如,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;
}

对于舒尔应该存在更好的方法来获得这个结果。

我找不到smt mongo doc来实现这个结果。

如何使用Mongo工具获得相同的效果。

1 个答案:

答案 0 :(得分: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是处理这类问题的选择。