谁能帮我将mongoDB聚合转换为spring数据mongo?
我正在尝试在每个邀请文档中获取未提醒参与者的电子邮件列表。
使它在mongo shell中工作,但需要在Spring数据mongo中进行。
我的shell查询
db.invitation.aggregate(
[
{ $match : {_id : {$in : [id1,id2,...]}}},
{ $unwind : "$attendees" },
{ $match : { "attendees.reminded" : false}},
{ $project : {_id : 1,"attendees.contact.email" : 1}},
{ $group : {
_id : "$_id",
emails : { $push : "$attendees.contact.email"}
}
}
]
)
如您所见,这就是我想出的,它在管道的项目和组操作中没有按预期工作。生成的查询如下。
聚合对象创建
Aggregation aggregation = newAggregation(
match(Criteria.where("_id").in(ids)),
unwind("$attendees"),
match(Criteria.where("attendees.reminded").is(false)),
project("_id","attendees.contact.email"),
group().push("_id").as("_id").push("attendees.contact.email").as("emails")
);
它创建以下查询
由聚合对象生成的查询
{ "aggregate" : "__collection__" , "pipeline" : [
{ "$match" : { "_id" : { "$in" : [id1,id2,...]}}},
{ "$unwind" : "$attendees"},
{ "$match" : { "attendees.reminded" : false}},
{ "$project" : { "_id" : 1 , "contact.email" : "$attendees.contact.email"}},
{ "$group" : { "_id" : { "$push" : "$_id"}, "emails" : { "$push" : "$attendees.contact.email"}}}]}
我不知道在Spring Data Mongo中使用Aggregation Group的正确方法。
有人可以帮我吗,或者提供链接到$ push等的组聚合?
最佳答案
正确的语法为:
Aggregation aggregation = newAggregation(
match(Criteria.where("_id").in(ids)),
unwind("$attendees"),
match(Criteria.where("attendees.reminded").is(false)),
project("_id","attendees.contact.email"),
group("_id").push("attendees.contact.email").as("emails")
);
参考:http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.group