我有一个Mongo集合(播放器),该集合引用了另一个集合(引导)。我想使用MongoTemplate聚合来获得所需的结果。
示例
玩家收藏
{
"_id" : ObjectId("5acb22865c7ba60005e6c24b"),
"name" : "Roberto Ayala",
"isRetired" : false,
"boot" : {
"$ref" : "Boots",
"$id" : ObjectId("5ab5fb6ac08f59b7397d3194")
}
}
靴子收藏
{
"_id" : ObjectId("5ab5fb6ac08f59b7397d3194"),
"model" : "F50",
"Sponsor" : "Adidas"
}
}
我要实现的目标:返回带有玩家名称和靴子型号的投影
代码
public Object Test(){
AggregationOptions options = Aggregation.newAggregationOptions().cursor(new BasicDBObject()).build();
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.project("name"));
//Additional Aggregation methods
return mongoTemplate.aggregate(aggregation,"Players",XYZ.class).getRawResults();
}
如何从“引导”字段中提取ID,然后投影“模型”字段?