【发布时间】:2015-12-17 10:47:59
【问题描述】:
谁能帮我把这个mongoDB聚合转换成spring data mongo?
我正在尝试在每个邀请文档中获取未提醒的与会者电子邮件列表。
让它在 mongo shell 中工作,但需要在 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"}
}
}
]
)
这就是我想出的,正如你所看到的,它在管道的项目和团队运营中没有按预期工作。生成的查询如下所示。
聚合对象创建
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中使用聚合组的正确方法。
有人可以帮我吗,或者使用 $push 等提供指向组聚合的链接?
【问题讨论】:
-
我遇到了类似how-to-aggregate-in-spring-data-mongo-db-a-nested-object-and-avoid-a-propertyException 的问题。但事实证明这是一个不同的解决方案
标签: java spring mongodb spring-data-mongodb