0

I want to query my mongo db and retrieve all the documents where their dates contain some value.

for example: my input is x = "17" , the query will return all the documents where the field date contains the string 17 . (08/03/2017) for example

I am using Criteria.where("date").regex("." + x + "."); This doesn't work (it only works on string fields)

larnouch
  • 189
  • 1
  • 15
  • It must be international regex on dates questions day, because third question in the last few hours. Giving the same response. – Neil Lunn Aug 08 '17 at 09:50
  • It's "much better" to actually convert the "strings" to a BSON Date and then [use a "range" to select between the dates](https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb) ( a single day is in fact a "range" ). Stored as a BSON Date, this in fact takes **4-bytes** ( length of the long integer value for the timestamp ) as compared to the **24-bytes** to store the "string". Not only does storage take more space, but that also equates to time to load and process, and it's a significant difference, especially over a large amount of data. – Neil Lunn Aug 08 '17 at 09:51
  • but how could i guess which is the date from a string like "17" (17 could be month , day , or years...) , if it were a date as an input I could use the range to select between the dates , but it is a string. – larnouch Aug 08 '17 at 10:03
  • The lesson here is to fix your dates. Storing as "strings" is just wrong. Once you fix your dates it's very easy to "range query" just as explained and linked to above. Don't waste time with strings. – Neil Lunn Aug 08 '17 at 10:06

0 Answers0