提供了两个mongo集合
collection - bacteria (id)
Coccus
Spirochete
Spirillum
Vibrio
Bacillus
collection - bacteria_relations (left_type, right_type, relation_type)
Coccus, Spirillum, can_cause
Coccus, Vibrio, can_cause
Coccus, Vibrio, infect
Bacillus, Spirillum, can_cause
如何获取与关联类型相关的所有细菌?
例如,所有与细菌有关的球菌,关联类型都可能是can_cause:
Spirillum
Vibrio
使用sql会很容易(伪代码)
select bacteria_relations.right_type from bacteria
left join bacteria_relations on bacteria.id=bacteria_relations.left_type
and relation_type='can_cause'
and bacteria.id ='Coccus'
我知道我需要使用LookupOperation和AggregationResults 这是我开始的伪代码:
LookupOperation lookupOperation = LookupOperation.newLookup()
.from("bacteria_relations")
.localField("left_type")
.foreignField("right_type").as("?");
AggregationResults<Object> entities =
mongoTemplate.aggregate(
Aggregation.newAggregation(lookupOperation,
Aggregation.match(Criteria.where("id").in(ids))),
Bacteria.class, Bacteria.class);