Golang parsing the isodate type in MongoDB

Source: Internet
Author: User
Tags cst time

You can use time in Golang. The time data type to hold isodate times in MongoDB.

G

Type Model struct {uploaddate time. Time ' Bson: ' uploaddate ' '}m: = model{}if Err: = C.find (nil). Select ({"_id": 0, "uploaddate": 1}). One (&m); Err! = Nil {fmt. Printf ("Failed to find date, error%s\n", err) OS. Exit (1)}fmt. Printf ("%+v\n", m)

From the output you can see that the time format of the Golang output is the CST time zone, Golang in the process of converting the ISO time to CST times, from the time panel is also more than 8 hours in MongoDB, this is normal.


So Golang do you need to convert the timestamp when you do the insert operation or time comparison operation? The answer is no need, look at the insert example.

Reinsert a record with the time field of the current period, which can be passed in Golang. Now to get the current time, view the output to see the time format as CST.

Now: = time. Now () Fmt. Printf ("%+v\n", now)//output:2016-05-12 14:34:00.998011694 +0800 csterr = C.insert (Model{time:now}) if err! = Nil {P Anic (ERR)}

You can see the automatic conversion of time stored into MongoDB for ISO time, 8 hours less time. A summary is that the time interaction in Golang and MongoDB does not need to be considered for additional things because the drivers are converted.


Sometimes we will take time. Time is stored as a string, so you need to convert it when you interact with MongoDB. Time format

TimeString: = "2016-05-12 14:34:00.998011694 +0800 CST" T, err: = time. Parse ("2006-01-02 15:04:05.999999999-0700 MST", timestring) if err! = Nil {panic (err)}fmt. Printf ("%+v\n", T)

What's hard to understand in the code is time. The first argument to parse, which is actually a definition in Golang, looks at time in detail. The source of the time.string () will understand.

This article from the "Linux related" blog, reproduced please contact the author!

Golang parsing the isodate type in MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.