如何更新MongoDB的值?
问题描述:
我必须为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工具实现相同的效果。
答
允许直接更新;不需要查找作为第一步,这可能会导致更新和找到操作之间的不一致。
@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
是处理这些类型的问题的选择。