javamongodbmorphia

morphia and howto update existing document field


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);  

Solution

  • 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);