mongo find query on joda datetime

2019-08-13 13:31发布

问题:

i am trying to use find query in mongodb to determine the records on that specific date , the query is working fine when i pass it the normal date object and if i find according to dateCreated field, but for joda date field, i don't know how should i form a joda date

right now i am using this query for normal records

var from =  new Date('2015-05-18T00:00:00.000Z'); 
var to =  new Date('2016-05-19T00:00:00.000Z'); 
db.delivery.find( {"dateCreated" : { $gte:from,$lt:to } } );

now i also have a field called deliveryDate which is of type joda date and stored like this

"deliveryDate" : {
    "jodaType" : "org.joda.time.DateTime",
    "interval_start" : ISODate("2015-03-28T18:30:00Z"),
    "interval_end" : ISODate("2015-03-28T18:30:00Z"),
    "jodaField_zone" : "UTC",
    "time" : NumberLong("1427567400000"),
    "jodaField_dayOfMonth" : 28,
    "jodaField_hourOfDay" : 18,
    "jodaField_minuteOfHour" : 30,
    "jodaField_millisOfSecond" : 0,
    "jodaField_monthOfYear" : 3,
    "jodaField_year" : 2015
},

i googled a lot but with no success, i have no idea how can i query for joda date ,please help!

回答1:

Your joda date is serialised into a nested JSON object. You should be able to query on it using interterval_start

db.delivery.find( {"deliveryDate.interval_start" : { $gte:from,$lt:to } } );