在春天过滤mongo文档中的子元素

时间:2017-05-09 10:58:18

标签: java spring mongodb

mongo Document看起来像这样

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

如果传递当前日期,我想过滤大于开始日期的结果 - 我不想要过去的schedule_dates对象。

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

在春季服务中

 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);

但它没有过滤 有人可以帮助我获取这些数据吗。

0 个答案:

没有答案