Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Vava到MongoDB的查询:查找特定日期范围内的事件_Java_Mongodb_Datetime_Mongodb Query - Fatal编程技术网

Java 从Vava到MongoDB的查询:查找特定日期范围内的事件

Java 从Vava到MongoDB的查询:查找特定日期范围内的事件,java,mongodb,datetime,mongodb-query,Java,Mongodb,Datetime,Mongodb Query,我在MongoDB中获得了以下文档: { "_id" : NumberLong(44), "_class" : "la.test.app.server.model.Event", "orgId" : NumberLong(2), "typeCode" : 1, "title" : "Test for notification", "shortDescription" : "Test for notification", "description" : "Test for notification

我在MongoDB中获得了以下文档:

{
"_id" : NumberLong(44),
"_class" : "la.test.app.server.model.Event",
"orgId" : NumberLong(2),
"typeCode" : 1,
"title" : "Test for notification",
"shortDescription" : "Test for notification",
"description" : "Test for notification",
"price" : "100",
"startDate" : ISODate("2015-02-08T16:30:07.000Z"),
"endDate" : ISODate("2015-02-09T16:00:07.000Z"),
"deleted" : false
}
我需要在所有其他事件中找到这个事件

我试图用方法做一件简单的事情:

public List<Event> getPendingEvents(Date start, Date end) {
   return mongoOperations.find(
        Query.query(Criteria
         .where("startDate").gte(start).lte(end)
         .and("typeCode").is("1")),

Event.class
);
这个查询只找到“Fields:null,Sort:null”:

但是直接查询MongoDB:

db.events.find({
"startDate" : {"$gte" : ISODate("2015-02-08 16:30:07.000Z"),
               "$lt" : ISODate("2015-02-08 16:31:07.000Z")},
"typeCode" : 1})

查找所需的事件。

是否尝试使用SimpleDataFormat…格式化日期

我很久以前就试过了

//   Find documents by date of birth
  Date gtDate = null;
  try {
   gtDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse("1984-05-010T8:30:00.000Z");
  } catch (ParseException e) {
   e.printStackTrace();
  }
  BasicDBObject dateQueryObj = new BasicDBObject("DateOfBirth",  new BasicDBObject("$lt", gtDate));
  cursor = documents.find(dateQueryObj);
  while(cursor.hasNext()) {
   System.out.println("\n Find documents by date of birth. \n");
      System.out.println(cursor.next());
  }

使用BasicDBObject而不是mongoOperations intreface解决了同样的问题

诸如此类:

BasicDBObject query = new BasicDBObject();
    query = new BasicDBObject("endDate", new BasicDBObject("$gte", start).append("$lte", end)).append("typeCode", 1);


    List<DBObject> result = collection.find(query).toArray();
BasicDBObject查询=新建BasicDBObject();
query=newBasicDBObject(“endDate”,newBasicDBObject($gte),start)。append($lte,end))。append(“typeCode”,1);
列表结果=collection.find(query.toArray();
BasicDBObject query = new BasicDBObject();
    query = new BasicDBObject("endDate", new BasicDBObject("$gte", start).append("$lte", end)).append("typeCode", 1);


    List<DBObject> result = collection.find(query).toArray();