我尝试使用Spring数据Mongo聚合库在mongo集合上创建投影。
假设我有这个集合:
{
"_id": "33",
"name": "Foo",
"payments": {
"years": [{
"year": 2017,
"quarters": [{
"quarter": 3,
"transactions": [{
"amount": 800,
"type": "VARIA"
"dayOfMonth": 10
}]
}]
}]
}
我使用这个mongo聚合查询:
db.customers.aggregate(
{ $match: { "_id": {$in:
["33", "80"]
} }},
{ $project: { "payments":{"years":{"quarters":{"transactions":{"amount":1,"type":1}}}} } },
{ $unwind: "$payments.years" },
{ $unwind: "$payments.years.quarters" },
{ $unwind: "$payments.years.quarters.transactions" },
{ $match: { "payments.years.quarters.transactions.amount": {$ne: "0.00" }} },
{ $group: { "_id": "$_id", "payments": {"$push": "$payments.years.quarters.transactions"} }}
)
但我现在正尝试在我的java应用程序中实现它。我使用spring-data-mongo。
我尝试这样做,但后来我收到的结果为零。
MatchOperation matchAllKlantenStage = Aggregation.match(new Criteria("_id").in(ids));
ProjectionOperation projectStage = project()
.and("payments.years.quarters.transactions")
.nested(bind("type","payments.years.quarters.transactions.type")
.and("amount", "payments.years.quarters.transactions.amount")
.and("dayOfMonth", "payments.years.quarters.transactions.dayOfMonth"));
Aggregation aggregation = newAggregation(
matchAllKlantenStage,
projectStage,
unwind("payments.years.quarters.transactions"),
match(new Criteria("payments.years.quarters.transactions.amount").ne("0.00")),
group().push("payments.years.quarters.transactions").as("payments")
);
任何人都可以帮我吗?
亲切的问候,
卡伊