MongoDB Morphia。仅更新新字段,但不更新旧字段

时间:2016-07-29 00:06:14

标签: mongodb morphia

我得到了Json的回应

{
    "id":"1090",
    "title" : "My User",
    "description" : "First User",
    "country" : "US",
    "state" : "FL",
    "city" : "Miam"
    "auth" : "Scott"
}

我需要编写一个更新,只更新已更改的字段。

如果更新的JSON看起来像

{
    "id":"1090",
    "title" : "New User",
    "description" : "First User",
    "country" : "US",
    "state" : "Texas",
    "city" : "Dallas"
    "auth" : "Scott"}

我可以在下面使用

Blog blogDB=datastore.find(Blog.class, "blog_ID", blog.getBlog_ID()).get();
Query<Blog> query = datastore.createQuery(Blog.class).field("_id").equal(blogDB.getId()); //Find the object that is in database

UpdateOperations<Blog> ops = datastore.createUpdateOperations(Blog.class).set("title", blog.getcountry()).set("country", blog.getcity()).set("city", ....

datastore.update(query, ops);

我不想编写如上所示的UpdateOperations。有没有更有效的方法?

1 个答案:

答案 0 :(得分:0)

Morphia没有脏状态跟踪,没有。您需要手动跟踪这些更改。

相关问题