Columns have types

I recently needed to import some CSV data into a Mongo DB database and while mongoimport makes it easy to import data from a variety of data sources, importing time stamps proved to be more complicated than I anticipated.

Initially, importing time stamps seemed like a trivial task, given that there is a way to describe a CSV field as a time stamp via the --columnsHaveTypes switch. The example in the docs seemed strange, though, because it didn't look like any date/time format one would expect and is shown as created.date(2006-01-02 15:04:05).

The accompanying link simply pointed to the Go source file for parsing and formatting time. Thinking that the docs suggested to use different formats from that page, I tried a few of those listed on the page, like RFC3339, but all these attempts came back with parsing errors for my ISO-8601 time stamps (e.g. 2015-09-13T08:59:00Z).

It took me a few tries and some wading through the Go source parsing time stamps to realize that Go creators didn't think conventional time stamp formats are as useful as special magic numbers that tell the parser how to interpret the input. That is, where a typical time format would say YYYY for a 4-digit year, Go expects 2006 and for a 2-digit year, it expects 06. Similarly, 03 stands for a zero-prefixed hour using the 12-hour clock and 15 stands for an hour using the 24-hour clock.

Turns out, the example in MongoDB docs wasn't showing a sample date, but showing a format in those Go magic numbers. There is no ISO-8601 format in Go examples and the one with a RFC-3339 time stamp has the time zone following the UTC designator Z for some reason, but I gave it a try anyway, with small changes, and it worked out just fine and imported all of my time stamps without errors. This is the final format that worked for me, where created is the field name - created.date(2006-01-02T15:04:05Z).

I left a comment on the Mongo DB docs page with a suggestion to clarify how Go date/time formats work and hopefully they will update it at some point.

Comments: