mongDB连表查询(MongoTemplate)

查询主表ChatRoom,从表Friend。

方法一:

第一次查询连表查询:

主表ChatRoom中字段owner与从表Friend中的字段toUser关联 

LookupOperation lookup = Aggregation.lookup("Friend", ChatRoom.Fields.owner, Friend.Fields.toUser, "FriendChatRoom1");

生成虚表FriendChatRoom1。

第二次查询连表查询:

主表ChatRoom中字段owner与从表Friend中的字段fromUser关联 
                                    
LookupOperation lookup1 = Aggregation.lookup("Friend", ChatRoom.Fields.owner, Friend.Fields.fromUser, "FriendChatRoom2");

生成虚表FriendChatRoom2

//筛选条件
Criteria criteria = new Criteria().orOperator(
        第一张虚表筛选条件
        Criteria.where("FriendChatRoom1.fromUser").is(user.getId()),
        第二张虚表筛选条件
        Criteria.where("FriendChatRoom2.toUser").is(user.getId())
);
主表筛选条件
criteria.and(ChatRoom.Fields.enable).is(true);                              MatchOperation match = Aggregation.match(criteria);                                                分页                                                                            AggregationOperation operationNumber = Aggregation.skip((req.getPageNumber()) * req.getPageSize());
AggregationOperation operationSize = Aggregation.limit(req.getPageSize());                           将所有条件顺序放入Aggregation开始查询                                                       Aggregation aggregation = Aggregation.newAggregation(lookup,lookup1,match,operationNumber,operationSize);
    public List<ChatRoom> findAllEnableByFriend(User user, RoomFriendsRequest req){
        //连表生成虚表1
        LookupOperation lookup = Aggregation.lookup("Friend", ChatRoom.Fields.owner, Friend.Fields.toUser,
                "FriendChatRoom1");
        //连表生成虚表2
        LookupOperation lookup1 = Aggregation.lookup("Friend", ChatRoom.Fields.owner, Friend.Fields.fromUser,
                "FriendChatRoom2");
        //筛选条件
        Criteria criteria = new Criteria().orOperator(
                Criteria.where("FriendChatRoom1.fromUser").is(user.getId()),
                Criteria.where("FriendChatRoom2.toUser").is(user.getId())
        );
        criteria.and(ChatRoom.Fields.enable).is(true);
        MatchOperation match = Aggregation.match(criteria);
        //分页
        AggregationOperation operationNumber = Aggregation.skip((req.getPageNumber()) * req.getPageSize());
        AggregationOperation operationSize = Aggregation.limit(req.getPageSize());
        //查询
        Aggregation aggregation = Aggregation.newAggregation(lookup,lookup1,match,operationNumber,operationSize);
        AggregationResults<ChatRoom> results = mMongoTemplate.aggregate(aggregation, "ChatRoom", ChatRoom.class);
        return results.getMappedResults();
    }

方法二:

MongoRepository使用in来查询两张表

1. 查询出从表的Id集合,再查主表中符合条件的在从表的Id集合中

    public List<String> findAllToUserByFromUser(String fromId) {
        Query query = Query.query(
                Criteria.where(Follow.Fields.fromUser).is(fromId)
        );
        query.fields().include(Follow.Fields.toUser);
        DistinctIterable<String> distinctIds = mMongoTemplate.getCollection(mMongoTemplate.getCollectionName(Follow.class))
                .distinct(Follow.Fields.toUser, query.getQueryObject(), String.class);
        return Lists.newArrayList(distinctIds);
    }
    public Page<ChatRoom> findAllEnableByFollow(RoomFriendsRequest req,List<String> followUserIds){
        PageRequest pageRequest = PageRequest.of(req.getPageNumber(), req.getPageSize(), Sort.Direction.DESC, ChatRoom.Fields.viewPerson);
        Page<ChatRoom> list = mChatRoomRepository.findByOwnerInAndEnable(followUserIds,true,pageRequest);
        return list;
    }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值