mongodb怎么实现多表联合查询
这篇文章主要介绍mongodb怎么实现多表联合查询,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
mongodb多表联查的方法:
1、首先用from进行关联从表名
2、用localField写出主从表关联字段
3、用as查询结果名
4、用Aggregation进行多条件查询
5、查询结果赋给results
6、用print()输出查询结果
代码如下:
LookupOperationlookupOperation=LookupOperation.newLookup().from("DYNC_EXT_TestInstanceItem").//关联从表名localField("partVersion").//主表关联字段foreignField("partVersion").//从表关联的字段as("result");//查询结果名AggregationOperationmatch=Aggregation.match(criteria);Aggregationaggregation=Aggregation.newAggregation(match,lookupOperation);//多条件List<Map>results=mongoTemplate.aggregate(aggregation,"DYNC_EXT_TestInstance",Map.class).getMappedResults();//上面的DYNC_EXT_TestInstance必须是查询的主表名System.out.println(JSON.toJSONString(results));
以上是mongodb怎么实现多表联合查询的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。