查询MongoDB中的嵌入式文档,Spring Boot

时间:2017-06-01 08:25:47

标签: mongodb spring-boot spring-data

对于每个Event MongoDB文档,我想找到字段participantList.attendDataTo的最小值和字段participantList.attendDataFrom的最大值

文件示例:

{
"_id" : ObjectId("592f569e9403751fd0b3ec13"),
"_class" : "info.wkolasa.app.MeetHelp.model.Event",
"eventId" : 2,
"eventTitle" : "Dzień dziecka",
"eventDateTo" : ISODate("2017-05-29T13:55:00.466Z"),
"eventDateFrom" : ISODate("2017-05-29T09:55:00.466Z"),
"minEventPartcipants" : 2,
"eventDateConfirmTo" : ISODate("2017-05-28T13:55:00.466Z"),
"location" : "Lindego 13a",
"minEventTime" : 6,
"participantList" : [ 
    {
        "name" : "Heniek",
        "surname" : "Kolasa",
        "email" : "heniek.kolasa@gmail.com",
        "attend" : 1,
        "attendDataFrom" : ISODate("2017-05-31T12:00:00.466Z"),
        "attendDataTo" : ISODate("2017-05-31T14:00:00.466Z")
    }, 
    {
        "name" : "adnieszka",
        "surname" : "Smyk",
        "email" : "agnieszka.kolasa@gmail.com",
        "attend" : 1,
        "attendDataFrom" : ISODate("2017-05-31T11:30:00.466Z"),
        "attendDataTo" : ISODate("2017-05-31T15:00:00.466Z")
    }
]}

为了测试目的,我创建了方法participantAvailability(),它根据每个文档中字段eventDateFrom中的值计算availabilityOfParticipants。我想做类似的事情,但是对于每个文档,字段为participantList.attendDataTo的最小值和字段participantList.attendDataFrom的最大值。

    @RequestMapping(method = RequestMethod.GET, value = "/participantAvailability")
public @ResponseBody long participantAvailability(){
    Query query = new Query();
    query.with(new Sort(Sort.Direction.ASC, "eventDateFrom"));
    query.limit(1);
    Event event = mongoTemplate.findOne(query, Event.class);

    Query query1 = new Query();
    query.with(new Sort(Sort.Direction.DESC, "eventDateFrom"));
    query.limit(1);
    Event event1 = mongoTemplate.findOne(query1, Event.class);
    long availabilityOfParticipants = (event1.getEventDateFrom().getTime() - event.getEventDateFrom().getTime())/ (24 * 60 * 60 * 1000);
    return availabilityOfParticipants;
}

@Document参与者:

@Document 
public class Participant {

private String name;
private String surname;
private String email;
private Integer attend;
private Date attendDataFrom;
private Date attendDataTo;
  //getters and setters

@Document Event:

@Document
public class Event {

@Id
private String id;
private Integer eventId;
private String eventTitle;
private Date eventDateTo;
private Date eventDateFrom;
private Integer minEventPartcipants; 
private Date eventDateConfirmTo;
private String location;
private Long minEventTime;
private List<Participant> participantList = new ArrayList<>(); 
     //getters and setters

1 个答案:

答案 0 :(得分:0)

对于Mongo查询,它是这样的:

db.event.aggregate(
[
{ $unwind: "$participantList" },
{ $group : { _id : "$_id", minAttendTo : { $min : "$participantList.attendDataTo" }, maxAttendFrom : { $max : "$participantList.attendDataFrom" } } },
{ $project : { eventId : 1, dateDifference : { $divide : [ { $subtract : [ "$minAttendTo", "$maxAttendFrom" ] }, 24 * 60 * 60 * 1000 ] } } } 
]
)

对于java代码,您可以获得this question's accepted answer

的帮助