morphia and howto update existing document field

Erik picture Erik · Oct 10, 2011 · Viewed 17.6k times · Source

Im completely new to MongoDb and Morphia and
trying to learn how to update my 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 howto 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..

// morphia default update is to update all the UserData document so howto update selected ones

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

Answer

aav picture aav · Oct 11, 2011

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