mongodb Querying for a Date Range when date is saved as string

后端 未结 1 616
甜味超标
甜味超标 2021-01-13 17:37

I\'m saving data into the bongo as bulk insert. The data that\'s an array of JSON object contain date, numeric, alphanumeric data all saved as string.

Sample Dat

1条回答
  •  旧时难觅i
    2021-01-13 17:52

    One last try at this, because you don't have a good record of accepting good advice.

    Your date formats as they stand are going to bite you. Even where trying to work around them. Here are the problems:

    • The format is not lexical. Which means that even with a string comparison operators like $gte, $lte are just not going to work. A lexical date would be "2012-01-04" in "yyyy-mm-dd" format. That would work with the operators.

    • You could look at $substr (and it's complete lack of documentation, search on SO for real usage) within aggregate but your date format is lacking the double digit form of day and month ie "04/01/2012", so that is going to blow up the positional nature of the operator. Also you would have to transform before any $match which means you blow up any chance of reducing your pipeline input, so you are stuck with not being able to filter your large resultset by date.

    It's a horrible case, but there really is no other practical solution to the large data problem here than to convert your dates. Strings in the form that you have just do not cut it. Either, in order of preference convert to:

    • BSON date
    • epoch timestamp as long (whatever)
    • Lexical string representation (as described)

    Your main case seems to be filtering, so updating the dataset is the only pratical alternative. Otherwise you are stuck with "paging" results and doing a lot of manual work, that could otherwise be done server side.

    0 讨论(0)
提交回复
热议问题