java - make morphia update document with entity properties instead of replacing it (keep not mapped attributes) -


given example document in mongodb collection:

{"_id": 100, "name": "user name", "sidefield": "some value"} 

and entity morphia:

@entity() public class user {   @id public long id;   public string name; } 

is possible update mongodb document following example user object

user user = new user(); user.id = 100; user.name = "new name"; 

so morphia not delete "sidefield" attribute?

by default when use morphia's datastore.save(...) method, morphia replaces whole document under given _id new one, built entity, deleting way attributes not mapped entity. know can make "manual" updates of selected fields, that's not why added object mapper project's dependencies.

finally, have dropped morphia , solved problem using mongodb java driver api 3.0.0 , jackson this:

string updatejson = .... //i json string via rest api // here, if want validate update use json schema  // or jackson (to map updatejson object , validate javax.validation) document updatedoc = document.parse(updatejson); document setupdate = new document("$set", updatedoc); mongodb.getcollection("coll").updateone(filters.eq("_id", someid), setupdate); 

of course works simple updates enough case.


Popular posts from this blog