2011-10-10 18 views
6

イムと
morphiaとHOWTOのMongoDBとMorphiaには完全に新しいアップデート既存のドキュメントフィールド

私は/このページからそれを行う方法を理解して見ることができない。(ここではいくつかのエラーである可能性があります)、そのページで

@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(); 
    } 
} 

私が見ることができない。
http://www.mongodb.org

マイドキュメントは、次のようになります彼らがどのように私のUserData特定のを持つ更新する方法を記述する場所uUid
のようにupdate UserData.statusのようにuUid=123567

これは私が使うべきだと思うものです:

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

// morphiaのデフォルトの更新は、私は、これが何をしたいと思い

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

答えて

7

すべてのUserDataのドキュメントので、HOWTOの更新選択されたものを更新することです:O更新する方法

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

ds.update(query, ops); 
+0

はい、なぜそのページには、私はその情報を持って指すものではありません。または、私はそれを見逃しましたか、これはコレクション内の文書に対処する通常の方法ではありませんか? – Erik

2

morphiaインターフェイスは少し不器用で、ドキュメントは明確ではありません...しかし、NLYシングル、特定の文書が実際に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); 

また、a different documentation pageは内部その面倒なクエリを隠すために巧妙な方法を示していますエンティティクラス自身:

@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; 
    } 
} 
+0

私はあなたが表示するクエリの隠れが好きです。 'loggedIn()'メソッドをどう呼びますか?私は特定の 'User'権限のためのクエリを作成する必要がありますか? 'loggedIn()'を呼び出しますか?私は 'User'オブジェクト全体を取り出す必要はないでしょうか、それともJavaコードを短縮することができますか? – Erik

+0

@Erik: 'LOGGEDIN()は' 'User'クラスの便利な方法です。これは、Mongoデータベースからの文書を持つ 'User'オブジェクトをすでに設定していることを前提としています。 全体 'User'オブジェクトを抜くことなく、チェックするための他の方法があります。 'User'クラスまたは' *ユーザーのDAO * 'クラスで実装する静的メソッドのように:http://code.google.com/p/morphia/wiki/DAOSupport – Leftium

関連する問題