How can I convert a Gregorian date to Jalali date in MongoDB?
I tried adding the javascript function mentioned in this answer to my MongoDB query in Metabase but it doesn’t support "$function" so I need a direct MongoDB query instead.
How can I convert a Gregorian date to Jalali date in MongoDB?
I tried adding the javascript function mentioned in this answer to my MongoDB query in Metabase but it doesn’t support "$function" so I need a direct MongoDB query instead.
2
Answers
Unfortunately, MongoDB does not have built-in support for converting between calendar systems like Jalali and Gregorian. One way to achieve this is to write a server-side script in a language like JavaScript that performs the conversion, and then call that script using the $where operator in your MongoDB query.
Here is an example of how you could convert a date field named "date_field" from Gregorian to Jalali using JavaScript and the $where operator:
Note that this is a simple example and it may not suit all your use cases, but it is a good starting point for your implementation.
Also note that the above script will return only the year of Jalali date, if you want to have more details of Jalali date like day and month, you need to modify the script and also the query respectively.
I hope this helps!
I had the same issue and ended up converting the code to a MongoDB query.
Here’s an example that converts the date "2022/03/02" to "1400/12/11".