5

I`m trying to query mongodb for documents where "date" is betwen two dates. Sample data is:

{
        "_id" : ObjectId("4fad0af6709fbc1d481c3e05"),
        "ID" : NumberLong("200930746205085696"),
        "text" : "Forgot my charger...:(",
        "date" : ISODate("2012-06-14T10:49:57Z"),
        "sentiment" : "NEG"
}

My Java code is:

DBCursor cursor = null;
DB db = connect();

    Date startDate = new Date(System.currentTimeMillis() - (long)(numOfTimePeriods+1)*time);
    Date endDate = new Date(System.currentTimeMillis() - (long)numOfTimePeriods*time);
    DBObject query = new BasicDBObject();
    query.put("date", new BasicDBObject("$gt", startDate).append("$lte", endDate));

    cursor = db.getCollection("status").find(query);

but the cursor object has no results.

Query object looks like this:

{ "date" : { "$gt" : { "$date" : "2012-05-15T00:16:15.184Z"} , "$lte" : { "$date" : "2012-06-14T10:16:15.184Z"}}}

I suspect the problem is the date representation in DB. Any suggestions on this?

andjelko
  • 403
  • 1
  • 5
  • 8
  • 1
    That should have worked. Can you double-check the data? – Thilo Jun 14 '12 at 09:19
  • Yes, you are right, it seems to work! The problem was the data, I queried the period for there are no documents to fetch. Silly me! Thanks for yout help! – andjelko Jun 14 '12 at 10:48
  • Just take a look at http://stackoverflow.com/questions/6840540/java-mongodb-query-by-date It's already solved there. – josete Oct 31 '12 at 16:50
  • Possible duplicate of [Java/MongoDB query by date](https://stackoverflow.com/questions/6840540/java-mongodb-query-by-date) – mlmireles Jan 12 '18 at 23:51
  • Possible duplicate of [Date query with ISODate in mongodb doesn't seem to work](https://stackoverflow.com/questions/19819870/date-query-with-isodate-in-mongodb-doesnt-seem-to-work) – Alper t. Turker Jan 13 '18 at 15:17

2 Answers2

1

That $date representation is just the toString representation in the Java driver, of a Date. It uses the strict JSON/BSON representation, rather than the extended 10gen BSON where values can be objects represented like they are in the shell. You shouldn't try to query in the shell using the toString output like that, because it won't work for a lot of cases.

JXue
  • 11
  • 1
-1

Aren't you supposed to use the and operator in your query ?

db.foo.find( { $and: [ { date: { $gt: startDate } }, {date : { $lt: endDate} }  ] } )
jocelyn
  • 788
  • 6
  • 12
  • No, I think the query was fine. I use a very similar one, too, and it works. – Thilo Jun 14 '12 at 09:55
  • $and is implcit on a property if you append values ? – jocelyn Jun 14 '12 at 10:02
  • http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%3C%2C%3C%3D%2C%3E%2C%3E%3D "You can also combine these operators to specify ranges" Not sure if this works in the general case (with other operators). If you have time, can you try to combine $gt with $nin or something? – Thilo Jun 14 '12 at 10:05
  • No, I tried $and operator, tried from cmd, too, but with no success. – andjelko Jun 14 '12 at 10:31
  • Code seems to work, I made a mistake in date range I queried. I`ll confirm it works when app finishes the proccessing and answer the question. Thanks for help! – andjelko Jun 14 '12 at 10:50