Erik
Erik

Reputation: 5119

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

Upvotes: 11

Views: 18745

Answers (2)

aav
aav

Reputation: 2489

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

Upvotes: 15

Leftium
Leftium

Reputation: 17903

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

Upvotes: 2

Related Questions