9

私はMongoDbとMorphiaにまったく慣れておらず
、ドキュメントを更新する方法を学ぼうとしています。

このページからそれを行う方法を見る/理解することはできません:
http ://www.mongodb.org

私のドキュメントは次のようになります:(ここでエラーが発生する可能性があります)

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

UserDataそのページでは、特定のLikeifを持っているuUid
私の更新方法を説明している場所を見ることができませupdate UserData.statusuUid=123567

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

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

// morphiaのデフォルトの更新では、すべてのUserDataドキュメントが更新されるため、選択したドキュメントを更新する方法

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

2 に答える 2

13

私はこれがあなたが望むものだと思います:

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

ds.update(query, ops);
于 2011-10-10T22:10:06.517 に答える
2

モルヒネのインターフェースは少し不器用で、ドキュメントは明確ではありません...しかし、単一の特定のドキュメントのみを更新する方法は、Erikが参照したページで実際に示されています:

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

また、別のドキュメントページには、エンティティクラス自体の中にその面倒なクエリを隠すための賢い方法が示されています。

@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;
   }
}
于 2011-11-11T05:33:13.703 に答える