将java中的时间戳与mongoDB时间戳进行比较以获取值的范围

时间:2017-02-08 11:40:43

标签: java mongodb

由于我已经找到了这个问题的多个答案,我在尝试之后并不相信,所以想再次讨论。这是我的DB:

"_id" : ObjectId("58510deae17ee5fc404a6f38"),
    "Interface_Name" : "SDE-CONTROLLER",
    "MsgCount" : "9798",
    "SubsystemType" : 2,
    "Mean" : "1224750",
    "Dest" : "CDN",
    "DataCenterId" : "d1@ssipl",
    "SRC" : "SDE",
    "peakMsgTimestamp" : "1481706908",
    "PeakMsgCount" : "3087",
    "SuccessMsgBytes" : "137172",
     "VmId" : "vm1",
    "TIMESTAMP" : ISODate("2016-12-14T09:16:26.459Z")

}

我的java代码是:

    long startTime = 0,endTime =0;
    Date startDateObj = new Date();
    Date endDateObj = new Date();
    MongoDatabase mongoDb = MongoDBUtil.getInstance().getDB();
    MongoCollection<Document> collection = mongoDb.getCollection(table);
    String startDate = voAppDto.getStartDate();
    String endDate = voAppDto.getEndDate();
    if (startDate.length() > 0) {
        calendarFrom.set(Calendar.YEAR, Integer.parseInt(startDate.split("-")[0]));
        calendarFrom.set(Calendar.MONTH, Integer.parseInt(startDate.split("-")[1]) - 1);
        calendarFrom.set(Calendar.DAY_OF_MONTH, Integer.parseInt(startDate.split("-")[2].substring(0, 2)));
        startTime = calendarFrom.getTimeInMillis();
        startDateObj.setTime(startTime);
    }
    if (endDate.length() > 0) {
        calendarTo.set(Calendar.YEAR, Integer.parseInt(endDate.split("-")[0]));
        calendarTo.set(Calendar.MONTH, Integer.parseInt(endDate.split("-")[1]) - 1);
        calendarTo.set(Calendar.DAY_OF_MONTH, Integer.parseInt(endDate.split("-")[2].substring(0, 2)));
        endTime = calendarTo.getTimeInMillis();
        endDateObj.setTime(endTime);
    }
   MongoCursor<Document> cursor = collection.find(and(eq("Interface_Name","SDE-CONTROLLER"),eq("DataCenterId","d1@ssipl"),lt("TIMESTAMP",endDateObj),gt("TIMESTAMP",startDateObj))).iterator();

我能够获得记录,但没有。在mongoDB中执行相同查询时记录不匹配。

0 个答案:

没有答案