Can Spring Data Mongo only update a dirty field in a document?

I started using spring-data-mongo for an application where there are a lot of things to keep.

We actually chose mangoes because it was so advertised. Currently, we are very dependent on spring, we found our life very easily using some of its functions (prestige for people with spring hard work).

But there is one: a document with over 60 fields . So my question about speed and scalability is that spring -data-mongo only updates dirty fields in mongo database , how does Hibernate do it? a bit like what was explained here on Arthur Ronald FD Garcia

thanks for reading this

+4
source share
1 answer

From the MongoDB documentation:

If the update argument contains only pairs of fields and values, the update () method replaces the existing document with the document in the update argument, except for the _id field.

In other words, updating MongoDB works exactly as described. So, if you have this document in a test colletion:

 { "_id" : "123", "oldfield" : "oldvalue" } 

And run the following in the Mongo CLI:

 db.test.update({"_id": "123"}, { newfield : "value" }) 

The new document will look like this:

 {"_id": "123", "newfield" : "value"} 

If you want to change only some fields, you can use the $ set operator, which sets the value for one or more fields. When you use spring data, you should use the MongoTemplate class to update:

 Query q = new Query(Criteria.where("_id").is("123")); Update u = Update .update("newfield", "value") .set("oldfield", "newvalue"); this.template.findAndModify(query, update, Entity.class); 

Here you can read about the $ set operator and mongodb update: http://docs.mongodb.org/manual/reference/operators/#_S_set http://docs.mongodb.org/manual/applications/update

+3
source

Source: https://habr.com/ru/post/1446222/


All Articles