简体   繁体   English

如何在不丢失 Spring Mongotemplate 项目中的数据的情况下更新 Mongo 中的文档

[英]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.

相关问题 如何在内部地图中通过参数找到mongo文档(更好地使用Spring MongoTemplate) - How to find mongo document by parrameter in internal map (better with Spring MongoTemplate) 如何使用spring mongo data api更新嵌入式mongo文档 - How to update embedded mongo document using spring mongo data api 如何使用mongotemplate(Spring)根据某些字段添加/更新mongo集合 - How to add/update the mongo collections based on some fields using mongotemplate(Spring) Spring 数据 MongoTemplate 更新嵌套数组失败 - Spring data MongoTemplate update nested array failing 在Spring Mongo中作为嵌套文档项目 - Project as nested document in spring mongo MongoTemplate 一次更新多个文档而不使用 saveAll() 或循环 - MongoTemplate update multiple Document with at once without using saveAll() or loops Spring Data + MongoTemplate-文档上的约束不会影响inserts \\ saves - Spring data + MongoTemplate - constraints on document won't affect inserts\saves Android SQLiteDatabase更新架构,不会丢失数据 - Android SQLiteDatabase update schema without loosing data 如何在没有 ObjectId 的情况下将 Mongo 与 Spring Data 一起使用 - How to use Mongo with Spring Data without ObjectId 如何在不使用 mongoTemplate.executeCommand(..) 的情况下使用 spring-data-mongodb 创建视图? - How do I create a view using spring-data-mongodb without resorting to mongoTemplate.executeCommand(..)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM