Create a new entity class

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "user_info")
public class UserInfo {

    @Id
    private String id;

    private String name;

    private List<Like> likes;

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Like{

        private String code;

        privateString name; }}Copy the code

Two, the basic method of use

1: Common New modification@RequestMapping(value = "add")
    public ResponseMessage add(a){
        List<UserInfo.Like> likes_1 = new ArrayList<>();
        UserInfo.Like like_1 = new  UserInfo.Like();
        like_1.setCode("PLAY");
        like_1.setName("Play");
        likes_1.add(like_1);
        UserInfo.Like like_2 = new  UserInfo.Like();
        like_2.setCode("RUN");
        like_2.setName("Running");
        likes_1.add(like_2);

        UserInfo userInfo = new UserInfo();
        userInfo.setId("101");
        userInfo.setName("Xiao Ming");
        userInfo.setLikes(likes_1);
        userInfoService.save(userInfo);
        List<UserInfo.Like> likes_2 = new ArrayList<>();
        UserInfo.Like like_2_1 = new  UserInfo.Like();
        like_2_1.setCode("PLAY");
        like_2_1.setName("Play");
        likes_2.add(like_2_1);
        UserInfo.Like like_2_2 = new  UserInfo.Like();
        like_2_2.setCode("SLEEP");
        like_2_2.setName("Sleep");
        likes_2.add(like_2_2);

        UserInfo userInfo2 = new UserInfo();
        userInfo2.setId("100");
        userInfo2.setName("Xiao Ming");
        userInfo2.setLikes(likes_2);
        userInfoService.save(userInfo2);

        return new ResponseMessage();
    }

2: Fuzzy query or exact match for common fields@RequestMapping(value = "query")
    public Object query(a){
        /*String search = "SLEEP"; Pattern pattern = Pattern.compile("^.*" + search + ".*$" , Pattern.CASE_INSENSITIVE); Query query = new Query(Criteria.where("likes.code").is("PLAY")); Query query = new Query(new Criteria().orOperator(Criteria.where("likes.code").regex(pattern),(Criteria.where("likes.code").regex("^.*RUN.*$")))); * /
        Query query = new Query(Criteria.where("likes.code").regex("^.*PLAY.*$"));
        return userInfoService.query(query);
    }

3: Updates exact fields according to conditions@RequestMapping(value = "update")
    public Object update(a){
        String search = "PLAY";
        Pattern pattern = Pattern.compile("^. *" + search + ". * $" , Pattern.CASE_INSENSITIVE);
        Query query = new Query();
        query.addCriteria(Criteria.where("likes.code").is(search));
        Update update = new Update();
        update.set("name"."Xiaoming change");
        update.set("likes.$.name"."Play around.");

        UserInfo.Like like_2_3 = new  UserInfo.Like();
        like_2_3.setCode("PLAY");
        like_2_3.setName("Play");
        Update. Push ("likes", loc); // addToSet does nothing if the data already exists, and push inserts the same data.
        update.addToSet("likes", like_2_3);
        userInfoService.modify(query,update);
        return null;
    }
Copy the code