cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Date filter in MongoDB snap

aditya_sharma
New Contributor III

Hi,

I am trying to filter the records from MongoDB Select snap using date filter but somehow it is not working. I am using below expression in condition. Please suggest if I am doing something wrong:

{$and:[{โ€œetl_dateโ€:{$gte:โ€œ2019-05-15T00:00:00.000โ€}},{โ€œetl_dateโ€:{$lte:โ€œ2019-05-15T23:59:00.000โ€}}]}

Thanks
Aditya

3 REPLIES 3

psathyanarayan
Employee
Employee

I am assuming that when you say it is not working, it is giving syntax error. I tried the exact same syntax on my local mongo db instance, it gives the syntax error with MongoDB compass which is a front end tool to connect to MongoDB directly.

I think it it does not like the letter โ€œTโ€ that separates the date from the time component. You can just remove it and try, it should work.

For example, in my table, I had a column create_time that I used as shown below:

{$and:[{โ€œcreate_timeโ€:{$gte:โ€œ2019-02-12 20:53:31.000โ€}}, {โ€œcreate_timeโ€:{$lte:โ€œ2019-02-12 20:53:31.000โ€}}]}

Hope this helps.

Try the following:

{โ€œINSERT_TIMESTAMPโ€: {โ€˜$gteโ€™: ISODate(โ€œ2018-09-01โ€), โ€˜$lteโ€™: ISODate(โ€œ2018-12-01โ€)}}

Based on MongoDB documentation it should be in the above format and the API is expecting the same from Snap

Thanks, it worked.