Mongodb + Java Drivers. Search by date range

前端 未结 4 1825
眼角桃花
眼角桃花 2020-12-18 07:03

This is my first shot at using Mongodb with the java drivers. I can query the database via command line using javascript and the Date() object, however, I am having trouble

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 07:31

    I have not used the Java driver for mongo before, but it seems that the query you have created is not correct.

    Query: { "created_on" : { "$gte" : { "$date" : "2012-12-06T05:00:00.000Z"} , "created_on" : { "$lt" : { "$date" : "2012-11-06T05:00:00.000Z"}}}}
    

    The query should in fact end up looking like:

    Query: { "created_on" : {$gte: start, $lt: end}}
    

    Where start and end are dates. It seems like the second time you refer to "created_on" is unnecessary and in fact might be breaking your query.

    NOTE: I have not had the chance to test out this theory, but I am working from http://cookbook.mongodb.org/patterns/date_range/ which seems to be very relevant to the question at hand.

提交回复
热议问题