Morphia 和 howto 更新现有文档字段

发布于 2024-12-09 01:01:40 字数 1272 浏览 0 评论 0原文

我正在尝试更新文档。

我无法从该页面看到/理解如何执行此操作:
http://www.mongodb.org

我的文档如下所示:(这里可能存在一些错误)

@Entity
public class UserData {
    
    private Date creationDate;
    private Date lastUpdateDate;
    
    @Id private ObjectId id;
    public String status= "";
    public String uUid= "";


    public UserData() {
        super();
        this.statistic = new Statistic();
        this.friendList = new FriendList();
    }

    @Embedded
    private Statistic statistic;
    @Embedded
    private FriendList friendList;

    @PrePersist
    public void prePersist() {
        this.creationDate = (creationDate == null) ? new Date() : creationDate;
        this.lastUpdateDate = (lastUpdateDate == null) ? creationDate : new Date();
    }
}

在该页面上,我看不到任何描述如何更新具有特定 uUidUserData 的地方。

就像“如果 uUid=123567 则更新 UserData.status”。

这就是我认为我应该使用的:

ops=datastore.createUpdateOperations(UserData.class).update("uUid").if uuid=foo..something more here..

它更新所有 UserData 文档,那么如何更新选定的文档呢?

datastore.update(datastore.createQuery(UserData.class), ops);  

I am trying to update a document.

I cannot see/understand how to do it from this page:
http://www.mongodb.org

My Document looks as following: (could be some error here)

@Entity
public class UserData {
    
    private Date creationDate;
    private Date lastUpdateDate;
    
    @Id private ObjectId id;
    public String status= "";
    public String uUid= "";


    public UserData() {
        super();
        this.statistic = new Statistic();
        this.friendList = new FriendList();
    }

    @Embedded
    private Statistic statistic;
    @Embedded
    private FriendList friendList;

    @PrePersist
    public void prePersist() {
        this.creationDate = (creationDate == null) ? new Date() : creationDate;
        this.lastUpdateDate = (lastUpdateDate == null) ? creationDate : new Date();
    }
}

On that page I cannot see any place where they describe how to update my UserData that has a specific uUid.

Like "update UserData.status if uUid=123567".

This is what I think I should use:

ops=datastore.createUpdateOperations(UserData.class).update("uUid").if uuid=foo..something more here..

It updates all the UserData documents so how to update selected ones?

datastore.update(datastore.createQuery(UserData.class), ops);  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揪着可爱 2024-12-16 01:01:40

我想这就是你想要的:

query = ds.createQuery(UserData.class).field("uUid").equal("1234");
ops = ds.createUpdateOperations(UserData.class).set("status", "active");

ds.update(query, ops);

I guess this is what you want:

query = ds.createQuery(UserData.class).field("uUid").equal("1234");
ops = ds.createUpdateOperations(UserData.class).set("status", "active");

ds.update(query, ops);
白龙吟 2024-12-16 01:01:40

吗啡界面有点笨拙,文档也不清楚...但是实际上在 Erik 引用的页面

// This query will be used in the samples to restrict the update operations to only the hotel we just created.
// If this was not supplied, by default the update() operates on all documents in the collection.
// We could use any field here but _id will be unique and mongodb by default puts an index on the _id field so this should be fast!
Query<Hotel> updateQuery = datastore.createQuery(Hotel.class).field("_id").equal(hotel.getId());

...

// change the name of the hotel
ops = datastore.createUpdateOperations(Hotel.class).set("name", "Fairmont Chateau Laurier");
datastore.update(updateQuery, ops);

另外,不同的文档页面展示了一种巧妙的方法来隐藏实体类中繁琐的查询本身:

@Entity
class User
{
   @Id private ObjectId id;
   private long lastLogin;
   //... other members

   private Query<User> queryToFindMe()
   {
      return datastore.createQuery(User.class).field(Mapper.ID_KEY).equal(id);
   }

   public void loggedIn()
   {
      long now = System.currentTimeMillis();
      UpdateOperations<User> ops = datastore.createUpdateOperations(User.class).set("lastLogin", now);
      ds.update(queryToFindMe(), ops);
      lastLogin = now;
   }
}

The morphia interface is a little clumsy and the docs aren't clear... but a method to update only a single, specific document is actually demonstrated on the page Erik referenced:

// This query will be used in the samples to restrict the update operations to only the hotel we just created.
// If this was not supplied, by default the update() operates on all documents in the collection.
// We could use any field here but _id will be unique and mongodb by default puts an index on the _id field so this should be fast!
Query<Hotel> updateQuery = datastore.createQuery(Hotel.class).field("_id").equal(hotel.getId());

...

// change the name of the hotel
ops = datastore.createUpdateOperations(Hotel.class).set("name", "Fairmont Chateau Laurier");
datastore.update(updateQuery, ops);

Also, a different documentation page shows a clever way to hide that cumbersome query inside the entity class itself:

@Entity
class User
{
   @Id private ObjectId id;
   private long lastLogin;
   //... other members

   private Query<User> queryToFindMe()
   {
      return datastore.createQuery(User.class).field(Mapper.ID_KEY).equal(id);
   }

   public void loggedIn()
   {
      long now = System.currentTimeMillis();
      UpdateOperations<User> ops = datastore.createUpdateOperations(User.class).set("lastLogin", now);
      ds.update(queryToFindMe(), ops);
      lastLogin = now;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文