我想从一个名为“人”的集合中检索日期介于

"14-11-2014" and "20-11-2014"  <-- These are both in string format (dd-mm-yyyy)




user: "Erik"


我的mongoDB

{
  "_id" : "546c9f26dbeaa7186ab042c4",          <------this one should NOT be retreived
  "Task: "Sometask"                                                because of the user
  "date" : "20-11-2014",
  "user" : "Dean"
},
{
  "_id" : "546caef6dbeaa7186ab042c5",          <--------- This one should be retreived
  "task": "A task",
  "date" : "20-11-2014",
  "user" : "Erik"
}
{
  "_id" : "546caef6dbeaa7186ab042c5",          <----- This one should NOT be retreived
  "task": "A task",                                         because of the date
  "date" : "13-11-2014",
  "user" : "Erik"
}


我正在使用Java Mongo Java驱动程序2.11.3

也许有一些使用BasicDBObject的解决方案?

我很好奇..谢谢

编辑

我在用着:

public static String findTimelines(String begin, String end, String username) throws UnknownHostException, JSONException{
    DBCollection dbCollection = checkConnection("timelines");
    BasicDBObject query = new BasicDBObject();
    query.put("date", BasicDBObjectBuilder.start("$gte", begin).add("$lte",end).get());
    query.put("user", username);
    dbCollection.find(query).sort(new BasicDBObject("date", -1));
    DBCursor cursor = dbCollection.find(query);
    return JSON.serialize(cursor);
}


在查询“ 28-11-2014”至“ 01-12-2014”之类的内容之前,它不会起作用,即使有日期为“ 30-11-2014”的对象也不会返回任何内容。我认为这是因为月份变化。
另外,当您执行以下操作时,该对象也会撤回:“ 28-11-2014”至“ 30-11-2014”,因为月份保持不变

请帮忙!

最佳答案

试试这个

BasicDBObject query = new BasicDBObject();
query.put("date", BasicDBObjectBuilder.start("$gte", fromDate).add("$lte", toDate).get());
collection.find(query).sort(new BasicDBObject("date", -1));

09-26 07:59