pymongo - Mongodb searching date ranges within a list of dates -


in mongodb,

if have date, , want query records have date later provided date can this:

collection.find({datefield:{$gt:somedate}}) 

and if want find records between date range, can $and lt , gt.

but datefield datefields, list of dates. , there following records datefield

datefields = [june 1, 2015 hhmmss, june 2, 2015 hhmmss, june 14, 2015 hhmmss] datefields = [june 1, 2015 hhmmss, june 3, 2015 hhmmss, june 8, 2016 hhmmss, june 17, 2015 hhmmss] 

how construct search fetch records dates between june 3 , june 7, second record. , if ranges not possible, can single search june 3, 2015 while disregarding hhmmss?

i think solve you're asking. use aggregate operation unwind arrays , treat each element separate value in document.

db.collection.aggregate( [ { $unwind : { "$datefields" } }, { $match : { datefields : { $gt : isodate("2015-06-02t00:00:01z"), $lte : isodate("2015-06-03t23:59:59z")} } } ]) 

that alone give "exploded view" of array results, 1 document per array element matches. if want collapse down can add $group stage.

if isn't you're asking give bit more detail?

for second half of question regarding "disregarding hhmmss", there number of questions answered on that. check out query mongodb on month, day, year... of datetime


Comments