【Quanyou app】Why use MongoDB database? — Query the best friend with the highest fate value and return relevant information

Table of contents

1. Why use MongoDB database?

2. Fate is the best friend

train of thought


1. Why use MongoDB database?

The circle (dynamic) function involved in this project, users will like and comment on the circle of friends; then as the number of users continues to increase, information such as comments, likes and collections will also continue to increase; relatively speaking, users read these information More, less writing, that is, more reading and less writing ; and it doesn’t matter if there is more or less data such as the number of comments/likes, it has no impact, and the value of the data is low ; and the follow-up also involves a function, the display of nearby people , will also involve the relevant data of geographical location ;

Therefore, based on the above, this project involves the processing of massive data, and read more and write less, the value of the stored data is low, and it also needs to include geographical location-related data, etc. If it is not appropriate to use mysql or redis at this time, it cannot Efficiently process massive amounts of data, all of which use the MongoDB database.

2. Fate is the best friend

Requirements: Query the best friend according to the highest fate value and return relevant information

train of thought

Step 1: Get the user id by parsing the token through the token interceptor;

@Service
public class TanhuaService {

    @DubboReference
    private RecommendUserApi recommendUserApi ;

    @DubboReference
    private UserInfoApi userInfoApi ;    

    //查询今日佳友数据
    public TodayBest todayBest() {
        //1、获取用户id
        Long userId = UserHeader.getUserId();
        //2、调用API查询
        RecommendUser recommendUser = recommendUserApi.queryWithMaxScore(userId);
        if (recommendUser == null){
            //没有好友,直接推荐一个用户为1的好友
            recommendUser = new RecommendUser();
            recommendUser.setUserId(1l);
            recommendUser.setScore(99d);
        }
        //3、将RecommendUser转化为TodayBest对象
        //查询出指定的用户对象
        UserInfo userInfo = userInfoApi.getById(recommendUser.getUserId());
        //数据封装
        TodayBest vo = TodayBest.init(userInfo,recommendUser);
        //4、返回
        return vo ;
    }

Step 2: Query the recommended friend table according to the user id, sort the obtained recommended friend list in reverse order according to the fate value, and obtain a piece of data with the highest fate value as today's best friend recommendation.

Note: use the MongoDB database to operate on the data

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
   private MongoTemplate mongoTemplate;
  
 /**
     * 根据用户id查询最佳好友
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //根据toUserId查询,根据评分score排序,获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = new Query(criteria);
        //排序:分数降序,分页第一条
        query.with(Sort.by("score").descending()).limit(1);
//      Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        RecommendUser one = mongoTemplate.findOne(query, RecommendUser.class);
        //调用mongoTemplate查询
        return one ;
    }
}

Step 3: Process the recent friend information, obtain the user id of the friend, and take the id to the user table to check the user information

Step 4: Define the encapsulation class, combine and assign the relevant information of the two tables and return it.

 TodayBest   Information Encapsulation—Best Friend Class

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TodayBest implements Serializable {
    private Long id ;   //用户id
    private String avatar ;     //头像
    private String nickname ;   //昵称
    private String gender ;     //性别
    private Integer age ;   //年龄
    private String[] tags ;     //标签
    private Long fateValue ;     //缘分值

    /**
     * 在vo对象中补充一个方法,封装转化过程
     * 将用户信息和推荐信息合并赋值
     * 由UserInfo转化为TodayBest
     */
    public static TodayBest init(UserInfo userInfo , RecommendUser recommendUser){
        TodayBest vo = new TodayBest();
        BeanUtils.copyProperties(userInfo,vo);
        //标签分割设置
        if (userInfo.getTags() != null){
            vo.setTags(userInfo.getTags().split(","));
        }
        //获取缘分值并设置
        long value = recommendUser.getScore().longValue();
        vo.setFateValue(value);
        return vo ;
    }
}

 Note: User information is stored in the Mysql database, and friend information is stored in the MongoDB database. When mutual data needs to be used, Dubbo is used to make remote calls.

Guess you like

Origin blog.csdn.net/zsy3757486/article/details/130268685