从 MongoDb 聚合查询创建 Spring 数据聚合
Create Spring Data Aggregation from MongoDb aggregation query
谁能帮我把这个 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 数据 mongo 中使用聚合组的正确方法 mongo。
有人可以帮助我或提供 link 以使用 $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
谁能帮我把这个 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 数据 mongo 中使用聚合组的正确方法 mongo。
有人可以帮助我或提供 link 以使用 $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