Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

morphia and howto update existing document field

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);  
like image 849
Erik Avatar asked Oct 10 '11 21:10

Erik


2 Answers

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);
like image 55
aav Avatar answered Nov 09 '22 23:11

aav


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;
   }
}
like image 35
Leftium Avatar answered Nov 10 '22 01:11

Leftium