3

最初、最後、または選択したオブジェクトを削除する方法しか見つかりませんが
、配列全体を削除する必要があります。

モルフィアでは、これを以下に持っていますDocument FriendList

Documentが表示されますarray friendList。これを new
で更新する必要があります。 array"friends"

friendList
新しい友達を 入力する前に、すべてのエントリを削除する必要があります。

私はそれを削除してから、単に新しい
array friendList含むを挿入できると考えていました"friends"

どうすれば削除できarrayますか?

解決策が見つからないので、これを行う方法が間違っている可能性があります..

@Entity
public class FriendList {

    @Id private ObjectId id;

    public Date lastAccessedDate;

    @Indexed(name="uuid", unique=true,dropDups=true)  
    private String uuid;


    List<String> friendList;

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public List<String> getFriendList() {
        return friendList;
    }

    public void insertFriend(String friend) {
        this.friendList.add(friend);
    }

}

ドキュメントから、運がないさまざまな組み合わせでこれを試します:

mongo.createUpdateOperations(FriendList.class).removeAll("friendList", "??");
4

2 に答える 2

2

unset メソッドを使用してから addAll または単に set を使用できます。

http://code.google.com/p/morphia/wiki/Updating#set/unset

次のようになります。

ops = datastore.createUpdateOperations(FriendList.class).unset("friendList");
datastore.update(updateQuery, ops);
ops = datastore.createUpdateOperations(FriendList.class).addAll("friendList", listOfFriends);
datastore.update(updateQuery, ops);

またはセットで:

ops = datastore.createUpdateOperations(FriendList.class).set("friendList", listOfFriends);
datastore.update(updateQuery, ops);
于 2011-12-11T16:05:47.813 に答える
-2

一般的には、一般的な (Java) リスト操作を使用するだけで済みます。リストをクリアするには、リストを null に設定し、必要に応じてエントリを削除または追加します。エンティティは非常に簡単です。

なぜあなたも持っているのmongo.createUpdateOperations(FriendList.class)ですか?オブジェクトが非常に大きい場合、単一のフィールドを更新するためにすべてをロードして保持したくない場合があります。ただし、単純なアプローチから始めて、必要に応じてより複雑なクエリのみを使用します。

時期尚早に最適化しないでください - 必要に応じてビルド、ベンチマーク、および最適化してください!

編集:

あなたのエンティティで:

public function clearFriends(){
    this.friendList = null;
}

必要な場所:

FriendList friendList = ...
friendList.clearFriends();
persistence.persist(friendList); // Assuming you have some kind of persistence service with a persist() method

または、unset などの特別な Morphia メソッドを使用することもできますが、これはやり過ぎかもしれません...

于 2011-12-11T13:00:05.877 に答える