[英]How to update document in Mongo without loosing data in a Spring Mongotemplate project
I am new to Spring and MongoDB.我是 Spring 和 MongoDB 的新手。 I am trying to update a document, but every time I update some fields, the others disappear.
我正在尝试更新文档,但是每次更新某些字段时,其他字段都会消失。
This is my current method:这是我目前的方法:
public UpdateResult updateCentro(String id, JsonNode jsonNode) {
ObjectId objectId = new ObjectId(id);
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(objectId));
Update update = Update.fromDocument(JsonNodeToDocumentConverter.INSTANCE.convert(jsonNode));
return mongoTemplate.updateFirst(query, update, "centros");
}
If the object that I give to the update method has 3 fields to be updated, in the database it leaves me those 3, but it deletes all the others.如果我提供给更新方法的 object 有 3 个要更新的字段,则在数据库中它会留下这 3 个字段,但会删除所有其他字段。
I am trying to do something like this, but using mongoTemplate in Spring:我正在尝试做这样的事情,但在 Spring 中使用 mongoTemplate:
db.foo.update({"_id" :ObjectId("4e93037bbf6f1dd3a0a9541a") },{$set : {"key1":"value1",""key2":"value2",....}})
If the object that I give to the update method has 3 fields to be updated, in the database it leaves me those 3, but it deletes all the others.
如果我提供给更新方法的 object 有 3 个要更新的字段,则在数据库中它会留下这 3 个字段,但会删除所有其他字段。
As I have mentioned in the comments above, that is the expected behavior of the Update.fromDocument()
method:正如我在上面的评论中提到的,这是
Update.fromDocument()
方法的预期行为:
Note, that this will set attributes directly and not use $set.
请注意,这将直接设置属性而不使用 $set。 This means fields not given in the Document will be nulled when executing the update.
这意味着在执行更新时,文档中未给出的字段将被清空。
So, use this to update the specific fields:因此,使用它来更新特定字段:
Query query = ...;
Update update = new Update().set("key-1", "value 1").set("key-2", "value 2");
UpdateResult result = mongoTemplate.updateFirst(query, update, Document.class, "collection_name");
Note the Document
is of type org.bson.Document
.注意
Document
的类型是org.bson.Document
。
Using some prasad's information, this is my solution:使用一些 prasad 的信息,这是我的解决方案:
public UpdateResult updateCentro(String id, JsonNode jsonNode) {
ObjectId objectId = new ObjectId(id);
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(objectId));
Update update = new Update();
Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> field = fields.next();
String key = field.getKey();
JsonNode value = field.getValue();
JsonNodeType type = value.getNodeType();
switch (type) {
case ARRAY:
System.out.println(key);
System.out.println(value);
List<JsonNode> list = new ArrayList<JsonNode>();
for (JsonNode arrayItem : value) {
list.add(arrayItem);
}
update.set(key, list);
break;
case BINARY:
break;
case BOOLEAN:
update.set(key, value.asBoolean());
break;
case MISSING:
break;
case NULL:
break;
case NUMBER:
update.set(key, value.asDouble());
break;
case OBJECT:
update.set(key, value);
break;
case POJO:
break;
case STRING:
update.set(key, value.asText());
break;
default:
break;
}
}
return mongoTemplate.updateFirst(query, update, "centros");
}
Thanks for the help.谢谢您的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.