springboot集成MongoDB实现多表查询

小编真的是~有点儿时间都献给工作了,这不这两天儿又弄了个类似论坛的东西,用的MongoDB数据库,帖子评论和回复分别弄了两张表~下面贴代码,Spring Data Mongodb(小编弄了两张表的联查,剩下的闲暇时间再弄)

首先pom.xml里要有Spring Data Mongodb的配置,如果要是在官网生成项目的话会直接带出来

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
//这里面我没用@Id,因为这个自动生成的id查询后传递到前台就变成了MongoDB:ObjectID本身的样子了,前台直接获取获不到,这里在set数据时我直接给id设置uuid了
//帖子评论表
@Data
@Document(collection = "DirectTrainPostLeaveMessage")
public class DirectTrainPostLeaveMessage {
    private String id;
    private String breedId;   //主题id(什么品种)
    private String postId;   //帖子id
    private String postTopic;   //帖子主题
    private String postTitle;   //帖子标题
    private String leaveMessage;   //留言内容
    private String leaveMessageId;   //留言人id
    private String leaveMessageMan;   //留言人
    private String leaveMessageTime;   //留言时间
    private int leaveMessagePraise;   //留言点赞
}

//回复评论表
@Data
@Document(collection = "DirectTrainPostLeaveMessageComment")
public class DirectTrainPostLeaveMessageComment {
    private String id;
    private String leaveId;   //留言id
    private String leaveMessageMan;   //留言人
    private String comment;   //评论内容
    private String commentId;   //评论人id
    private String commentMan;   //评论人
    private String commentTime;   //评论时间
    private int commentPraise;   //评论点赞
}

访问controller方法获取数据

@RequestMapping(value = {"/getLeaveMessage"}, produces = {"application/json;charset=UTF-8"})
@ResponseBody
public BaseResult getLeaveMessage () {
    LookupOperation lookupOperation = LookupOperation.newLookup().from("DirectTrainPostLeaveMessageComment")   //从表名
                .localField("_id")   //主表关联字段
                .foreignField("leaveId")   //从表关联字段
                .as("CommentList");   //查询结果名
    //匹配id条件
    MatchOperation matchOperation = new MatchOperation(Criteria.where("postId").is(postId));
    //按回帖时间排序
    SortOperation sortOperation = new SortOperation(Sort.by(Sort.Order.desc("leaveMessageTime")));
    Aggregation aggregation = Aggregation.newAggregation(lookupOperation);
    List<Map> result = mongoTemplate.aggregate(aggregation, "DirectTrainPostLeaveMessage", Map.class).getMappedResults();
    //DirectTrainPostLeaveMessage是主表名
    return BaseResult.ok(result);
}

 上面的java语句切换成MongoDB命令是这样儿滴~(要说这个nosql刚刚写起来也是蹩手)

db.DirectTrainPostLeaveMessage.aggregate([   //db后面的是主表名
    {
        $lookup: 
        {
            from: "DirectTrainPostLeaveMessageComment",   //从表名
            localField: "_id",   //主表关联字段
            foreignField: "leaveId",   //从表关联字段
            as: "CommentList"   //查询结果名
        }
    },
    {
        $match:
	{
	    postId : "5e85a5d5e79d53eaacce974c"
	}
    },
	{
	$sort:
	{
	    leaveMessageTime : -1   //排序:升序:1,降序:-1
	}
    }
])

数据返回效果:

页面渲染效果:

over~有不足还请指教~

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值