0

For each Event MongoDB document I want to find minimum value of field participantList.attendDataTo and max value of field participantList.attendDataFrom

Example of document:

{
"_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")
    }
]}

for test purpose i created method participantAvailability() which calculate availabilityOfParticipants based on value in field eventDateFrom in each document. I want to do something like that but for minimum value of field participantList.attendDataTo and max value of field participantList.attendDataFrom for each document.

    @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 Participant:

@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
jocom
  • 69
  • 1
  • 3
  • 13
  • so what is the problem you are facing – pvpkiran Jun 01 '17 at 09:14
  • I don't know how to do this. I'm new in Spring Boot and MongoDB, I tryed for two days using Query class, Spring Data but with no result. – jocom Jun 01 '17 at 10:14
  • On MongoDB I wrote query which retrieve me data which I looking for. How to wrote it in Java?`db.event.aggregate([ { $unwind: "$participantList" }, { $group: { _id: "$_id", value: { $max: "$participantList.attendDataFrom" } } } ]);` – jocom Jun 01 '17 at 11:04
  • Don't you need minimum attendDataTo also? – barbakini Jun 01 '17 at 11:08
  • Yes, sorry for mistake – jocom Jun 01 '17 at 11:38
  • This is your correct and complete query i believe: 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 ] } } } ] ) – barbakini Jun 01 '17 at 11:40

1 Answers1

0

For Mongo query it is like this:

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 ] } } } 
]
)

And for java code you can get help with this question's accepted answer

barbakini
  • 3,024
  • 2
  • 19
  • 25