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多表联查的方法步骤是什么"的内容,大家可以关注恒创科技的其它相关文章。