0

好的,我有这个(又是初学者)

// ADD FRIEND TO FRIEND LIST   
Query<FriendList> query1 = mongo.createQuery(FriendList.class);   
query1.field("lowerCaseUserName").equal(on.lowerCaseUserName);  
query1.field("passwordHash").equal(on.passwordHash);
query1.field("uuid").equal(on.uuid);
UpdateOperations<FriendList>up1=mongo.createUpdateOperations(FriendList.class).add("friendList",buddyUuid,false);

我将一个朋友插入Array. “朋友列表”是一个String Array.
希望现在能够实施删除。

我可以只编写相同的代码并.add用 removexxx 替换“”...吗?我认为这是一个好主意,但也许不是:)

@Entity
public class FriendList {

    @Id private ObjectId id;

    public Date lastAccessedDate;

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

    @Indexed(value=IndexDirection.ASC, name="lowerCaseUserName", unique=true,dropDups=true)  
    public String lowerCaseUserName;

    public String passwordHash = "";

    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);
    }
//  @PrePersist void prePersist() {
//      lastAccessedDate = new Date();
//  }
}
4

1 回答 1

1
Query<FriendList> query1 = mongo.createQuery(FriendList.class);   
query1.field("lowerCaseUserName").equal(on.lowerCaseUserName);  
query1.field("passwordHash").equal(on.passwordHash);
query1.field("uuid").equal(on.uuid);
UpdateOperations<FriendList>up1=mongo.createUpdateOperations(FriendList.class).removeAll("friendList",buddyUuid);

这应该buddyUuid从列表中删除。

如果您可以保证friendList包含唯一的 UUID,那么您可以使用removeFirst || removeLast方法。

删除第一个/最后一个/全部

希望这可以帮助!

于 2011-10-23T20:17:33.593 回答