mongodb怎么多表联查?
 
                     Ly
2020-06-10 14:15:226106浏览 · 0收藏 · 0评论

mongodb多表联查的方法:
1、首先用from进行关联从表名
2、用localField写出主从表关联字段
3、用as查询结果名
4、用Aggregation进行多条件查询
5、查询结果赋给results
6、用print()输出查询结果
代码如下:
LookupOperation lookupOperation=LookupOperation.newLookup().
                    from("DYNC_EXT_TestInstanceItem").  //关联从表名
                    localField("partVersion").     //主表关联字段
                    foreignField("partVersion").//从表关联的字段
                    as("result");   //查询结果名
AggregationOperation match = Aggregation.match(criteria);
Aggregation aggregation=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));                 
                    关注公众号,随时随地在线学习

