我有一个game
的文档结构,其中有一个rounds
数组,每个回合都有一个用户的attends
数组,每个参与者都有一个uid
字段。像这样:
{
rounds: [
{
attends: [
{
uid:1
},{
uid:2
}
]
},{
attends: [
{
uid:1
},{
uid:2
}
]
}
]
}
现在,我要查询一个用户的出席数据。我知道如何使用shell
这样查询此问题:
db.game.aggregate([
{ $project: {
rounds: {
$map: {
input: "$rounds",as: "round",in: {
attends: {
$filter: {
input: "$$round.attends",as: "attend",cond: {
$eq: ["$$attend.uid",1]
}
}
}
}
}
}
}}
])
而且我知道如何使用mongo driver
来实现这一点,只需将其一一转换为Java。
但是,我无法以spring-data
样式实现等效代码:
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.project().and(VariableOperators.mapItemsOf(......
);
mongoTemplate.aggregate(aggregation,Game.class,Game.class);
有人可以帮助我如何以$filter
样式实现$map
和spring-data
吗?