SergejJurecko/erlmongo

Question about supported data types

tgrk opened this issue · 7 comments

tgrk commented

Hello, I looking for an information about supported data types. I used only strings and it worked just fine, but my question is related to date/time data type. How can I filter/sort based on column with data/time information? I am assuming that using string is not a proper way.

Thank you,
Martin

If you save [{"name",now()}] it will save as time value. There is also a function in mongodb module: datetime_to_now/1 which converts a {Date,Time} tuple to now.

You can also query with modifiers: [{"someval",{lte,mongodb:datetime_to_now(erlang:localtime())}}]

tgrk commented

Ok but I am not sure if I understand "tuple to now". Is it possible to parse for example date in past and insert it as date time mongodb type?

Thank you,
Martin

Lookup the erlang:now() function. It's microseconds since epoch. Basically the same thing mongo uses (they use miliseconds since epoch).

If you wan to save as a mongodb timestamp value, save in now() format for time. How you get there is up to you.

tgrk commented

Thank you for your explanation.

tgrk commented

I have another related question. mongodb:datetime_to_now() works great for inserts, but is there any function to for formatting saved timestamp value into readable format? I played with io_lib:format but without luck.

Thank you,
Martin

I'm not quite sure what you mean. The calendar module has functions:
now_to_local_time
now_to_universal_time

Which return now in {date(),time()} format. After that the only other function that I know of that parses that format is: httpd_util:rfc1123_date({date(),time()}. If you want to put it into some other form, you're on your own.

tgrk commented

I really did not understand "now". It is crystal clear now and it is working like a charm. Thank you for your help and patience :)