How to query date saved as text in bad date format in mongoDB -


i new mongodb have database sale_date , value saved text , format "dd:mm:yyyy". want query based on date. want query last month's entry. have field sale_time , saved text , format "hh:mm" , want query last hour's entry.

**i want query java , mongo console.

one row of collection:

{ "_id" : 112350, "sale_date" : "21.07.2011", "sale_time" : "18:50", "store_id" : "ok3889-45", "region_code" : 45, "product_id" : "qkdglhx5061", "product_catagorie" : 53, "no_of_product" : 1, "price" : 1211.37, "total_price" : 1211.37 }

i have million of entries. want find entries month of july 2011 or hour 18:00 19:00 in 21.07.2013.

you can query regex matching results. said format dd:mm:yyyy example looks dd.mm.yyyy used in examples

for example:

db.sales.find({sale_date: /..\.07\.2011/}) 

this ineficient since can't use index, job done.

it better, if stick dates strings reverse order yyyy:mm:dd use anchored regex, hit index like:

db.sales.find({sale_date: /2011\.07/}) 

for hour query:

db.sales.find({sale_date: "21.07.2013", sale_time: {$gte: "18:00", $lt: "19:00"}}) 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -