1

mongo Document looks like this

 {
"_id" : ObjectId("59118981125ad5448ccc5684"),
"type" : "Single",
"name" : "Gift User A",
"duration" : NumberLong(30),
"schedule_dates" : [ 
    {
        "sessionId" : "15161b641035e09b",
        "start_date" : ISODate("2017-05-09T10:18:00.000Z"),
        "end_date" : ISODate("2017-05-09T10:48:00.000Z"),
        "status" : "EXPIRED"
    }, 
    {
        "sessionId" : "bd631f8564d8a612",
        "start_date" : ISODate("2017-05-10T10:18:00.000Z"),
        "end_date" : ISODate("2017-05-10T10:48:00.000Z"),
        "status" : "OPEN"
    }, 
    {
        "sessionId" : "bd631f8564d8a612",
        "start_date" : ISODate("2017-05-11T10:18:00.000Z"),
        "end_date" : ISODate("2017-05-11T10:48:00.000Z"),
        "status" : "OPEN"
    }
]
}

i want to filter the result with greater than start date if pass present date - i dont want past schedule_dates objects.

{
"_id" : ObjectId("59118981125ad5448ccc5684"),
"type" : "Single",
"name" : "Gift User A",
"duration" : NumberLong(30),
"schedule_dates" : [ 
    {
        "sessionId" : "bd631f8564d8a612",
        "start_date" : ISODate("2017-05-10T10:18:00.000Z"),
        "end_date" : ISODate("2017-05-10T10:48:00.000Z"),
        "status" : "OPEN"
    }, 
    {
        "sessionId" : "bd631f8564d8a612",
        "start_date" : ISODate("2017-05-11T10:18:00.000Z"),
        "end_date" : ISODate("2017-05-11T10:48:00.000Z"),
        "status" : "OPEN"
    }
]
}

in spring service

 Query upComingSessionQuery = new Query();
    upComingSessionQuery.with(new Sort(Sort.Direction.ASC, "scheduleDates.startDate"));

    Criteria criteria = Criteria.where("userid").in(userService.getUserWithAuthorities().getId()).andOperator(Criteria.where("scheduleDates").elemMatch(Criteria.where("startDate").gt(todayDate)));
    upComingSessionQuery.addCriteria(criteria);
    List<Schedule> schedules = mongoOps.find(upComingSessionQuery, Schedule.class);

but it is not filtering.
Can someone help me to get these data-only.

Azzabi Haythem
  • 2,318
  • 7
  • 26
  • 32
  • 1. in your criteria you have mentioned scheduledDates whereas the field in db is schedule_dates. Check if this is typo in the question or your code :) 2. You can simplify your criteria like this Criteria criteria = Criteria.where("userid").in(userService.getUserWithAuthorities().getId()).andOperator(Criteria.where("schedule_dates.start_date").gt(todayDate))); – pvpkiran May 09 '17 at 11:13
  • @Field("schedule_dates") private List scheduleDates; i am declaring like this, using mongodb aggregationss query it is woking but i want spring query db.schedule.aggregate([ { "$unwind": "$schedule_dates" }, { "$match": { "schedule_dates.start_date": { $gte : new ISODate("2017-05-15T11:19:00.000Z") } }}, { "$group": { "_id": "$_id", "name": { "$first": "$name" }, "schedule_dates": { "$push": "$schedule_dates" } }} ]) – Java Coffee Bean May 09 '17 at 11:16
  • you have to give field names as it is in Database when using mongoOps. If you are using MongoRepository then what you use is field names specified using @Field – pvpkiran May 09 '17 at 11:22
  • here iam using MongoOperations interface to call db queries.everything is working fine but when startDate is greaterthan currentdate returning the current object. – Java Coffee Bean May 09 '17 at 12:15

0 Answers0