How to use moment.js and moment-timezone in MongoDB shell
I’ll introduce how to load() and use moment.js and moment-timezone in MongoDB shell.
First, download moment.js and moment-timezone-with-data-*.js files from the official site.
At the time of writing, moment-timezone-with-data-*.js was moment-timezone-with-data-2012-2022.js.
moment-timezone-with-data-2012-2022.js contains timezone data within the file, so this data is necessary when you want to handle timezones accurately from mongo shell.
By the way, the files are also available in the following GitHub directories:
Load() and use the downloaded moment.js and moment-timezone-with-data-*.js in mongo shell.
$ mongo yourdbname
> load("moment.js")
true
> load("moment-timezone-with-data-2012-2022.js")
true
> moment("2017-05-02 00:00:00").tz("America/Los_Angeles").format("YYYY-MM-DD HH:mm:ss")
2017-05-01 08:00:00
Alternatively, writing the load() process in mongorc.js is recommended since you can always use it.
mongorc.js
load("/path/to/moment.js");
load("/path/to/moment-timezone-with-data-2012-2022.js");
Please make use of moment.js and moment-timezone.js for a comfortable MongoDB life.
That’s all from the Gemba.