我在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
);
汇编并返回查询:
{
"startDate" : { "$gte" : { "$date" : "2015-02-08T05:29:00.000Z"} ,
"$lte" : { "$date" : "2015-02-08T05:31:00.000Z"}} ,
"typeCode":"1"
}
此查询只找到“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})
查找所需的活动。
答案 0 :(得分:0)
您是否尝试过使用SimpleDateFormat格式化日期......?
我曾经尝试过这么久......
// 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());
}
答案 1 :(得分:0)
使用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();