Spring Boot Mongo模板查询追加到列表

时间:2019-06-22 09:59:36

标签: mongodb spring-boot mongotemplate

我必须编写mongotemplate查询才能将数据追加到现有列表中,

以下是我的查询

Update update = new Update();
if (user != null) {

    update.addToSet("successList", user.getName());

    UpdateResult upResult = null;
    query = new Query(
        new Criteria().where("userId").is(user.getGroupId()));
    try {
    upResult = mongoTemplate.updateFirst(query, update, User.class);
    }
    catch (Exception e) {
    logger.info("Update user  request is failed", e);
    }

}

下面是模型

@Document(collection = "userDetails")
public class user implements Serializable {

    private static final long serialVersionUID = 89796876868761L;

    private String groupID;
    private String groupDescription;
    private List<String> names;

}

当前它已被覆盖。

{ "_id" : ObjectId("5d0df82c9ef320261c29a753"), "groupID" : "11111111111", "groupDescription" : "my users", "names" : [ "user1" ] }

{ "_id" : ObjectId("5d0df82c9ef320261c29a753"), "groupID" : "11111111111", "groupDescription" : "my users", "names" : [ "user2" ] }

我希望它像下面这样追加。

{ "_id" : ObjectId("5d0df82c9ef320261c29a753"), "groupID" : "11111111111", "groupDescription" : "my users", "names" : [ "user1", "user2" ] }

是否可以将其他查询追加到现有列表?

0 个答案:

没有答案