mongodb DBObject中的大于日期条件

时间:2017-05-05 07:28:00

标签: spring mongodb spring-mongodb

mongodb对象看起来像这样

{
"_id" : ObjectId("590acbe6125ad519801a4214"),
"name" : "Dev",
"duration" : NumberLong(30),
"schedule_type" : "DAILY",
"schedule_dates" : [ 
    {
        "startDate" : "2017-05-06T07:36:00.000Z",
        "endDate" : "2017-05-06T08:06:00.000Z",
        "status" : "OPEN",
        "sessionId" : "618274137cdc06e7"
    }, 
    {
        "startDate" : "2017-05-07T07:36:00.000Z",
        "endDate" : "2017-05-07T08:06:00.000Z",
        "status" : "OPEN",
        "sessionId" : "618274137cdc06e7"
    }, 
    {
        "startDate" : "2017-05-08T07:36:00.000Z",
        "endDate" : "2017-05-08T08:06:00.000Z",
        "status" : "OPEN",
        "sessionId" : "618274137cdc06e7"
    }
 ] }

in java 
DBCollection coll = mongoOps.getCollection("schedule");
DBObject unwind = new BasicDBObject("$unwind","$schedule_dates" );
DBObject match = new BasicDBObject("$match", new BasicDBObject( "schedule_dates.status" , "OPEN"));
DBObject sort = new BasicDBObject( "$sort", new BasicDBObject( "schedule_dates.startDate" , 1));
AggregationOutput output = coll.aggregate(unwind,match,sort);
得到这样的结果后

  [ {
    "scheduleDates": {
        "startDate": "2017-05-06T07:36:00.000Z",
        "endDate": "2017-05-06T08:06:00.000Z",
        "status": "OPEN",
        "sessionId": "618274137cdc06e7"
    },
    "name": "Dev",
    "status": "OPEN"
},
{
    "scheduleDates": {
        "startDate": "2017-05-07T07:36:00.000Z",
        "endDate": "2017-05-07T08:06:00.000Z",
        "status": "OPEN",
        "sessionId": "618274137cdc06e7"
    },
    "name": "Dev",
    "status": "OPEN"
},
{
    "scheduleDates": {
        "startDate": "2017-05-08T07:36:00.000Z",
        "endDate": "2017-05-08T08:06:00.000Z",
        "status": "OPEN",
        "sessionId": "618274137cdc06e7"
    },
    "name": "Dev",
    "status": "OPEN"
}]

在此我应用where条件来过滤基于startDate的记录,其中条件失败
条件是:开始日期大于当前日期。

 DBObject where = new BasicDBObject("$where", new BasicDBObject("$gt",new BasicDBObject("schedule_dates.startDate", todayDate)));

0 个答案:

没有答案