从MongoDb聚合查询创建Spring数据聚合 [英] Create Spring Data Aggregation from MongoDb aggregation query
问题描述
任何人都可以帮助我将此mongoDB聚合转换为spring数据mongo吗?
Can any one help me convert this mongoDB aggregation to spring data mongo?
我试图在每个邀请文档中获取未提醒与会者的电子邮件列表.
I am trying to get list of un-reminded attendee's email in each invitation document.
让它可以在mongo shell中工作,但需要在Spring数据mongo中进行.
Got it working in mongo shell, but need to do it in Spring data 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"}
}
}
]
)
这是我想出的,正如您所看到的,它在管道的项目和组操作中没有按预期工作.生成的查询如下.
This is what I came up with, as you can see, it's working not as expected at a project and group operation of the pipeline. Generated query is given below.
创建聚合对象
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数据mongo中使用聚合组的正确方法.
I don't know the correct way to use Aggregation Group in spring data mongo.
有人可以帮我吗,或者提供链接到$ push等的群组聚合?
Could someone help me please or provide link to group aggregation with $push etc?
推荐答案
正确的语法为:
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
这篇关于从MongoDb聚合查询创建Spring数据聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!