Is there a difference of 8 hours between the time of insertion and the actual time found in Mongovue?

Source: Internet
Author: User

In the process of mongodb use, we found that the problem is that when we insert the date type, we find that the data saved to MongoDB is 8 hours late. I think that when you use client tools such as Mongovue, you will also find that the data queried is also 8 hours late? Below is a brief description of the reason.

The above two questions, in turn, we see why?

(1) In the Java encoding process, when using the MongoDB Java driver package to save data, the data in the database is 8 hours less than the actual time, why?

In order to find out why, I downloaded a mongodb Java driver package, I would like to find the reason through the driver package, the driver packet is as follows:

As we all know, the MongoDB database is document-based and each document is in a JSON-like format, so there must be some sort of JSON serialization when it's saved to the database, and we're going to find the relevant serialization class in that direction, We see a JSON class under the Com.mongodb.util package that has the following logic to serialize in this class:

public static String Serialize (Object object)  {    StringBuilder buf = new StringBuilder ();    Serialize (object, buf);    return buf.tostring ();  }  public static void Serialize (Object object, StringBuilder buf)  {    jsonserializers.getlegacy (). Serialize ( object, buf);  }

It can be seen that the corresponding serialization class is first found for any type (a bit like the implementation of Hessian, Fastjson), and then the corresponding serialization class is called to serialize the object objects.

We see this within the Jsonserializers class:

Serializer.addobjectserializer (Date.class, New Legacydateserializer (serializer));

This sentence is used to bind the java.util.Date type to the Legacydateserializer serialization class and use that class to serialize the Java.utl.Date type, and we'll see how it's serialized:

private static class Legacydateserializer extends Jsonserializers.compoundobjectserializer  {    Legacydateserializer (Objectserializer Serializer)    {      super ();    }    public void serialize (Object obj, StringBuilder buf)    {      Date d = (date) obj;      SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd ' T ' HH:mm:ss. SSS ' Z ');      Format.setcalendar (New GregorianCalendar (new SimpleTimeZone (0, "GMT"));      This.serializer.serialize (New Basicdbobject ("$date", Format.format (d)), buf);    }  }

From the code can be seen, in fact, for our date type is converted to the international standard Time GMT, and our time zone is the East Eight (gmt+8), so after conversion to a time less than 8 hours, in the database see also less 8 hours.

Now that we know the reason, does this situation affect our business? There is no effect, because the Java driver of MongoDB also does the corresponding conversion work for us when we read it.


(2) When using the Mongovue client software to manipulate the MongoDB database, the data and the actual save time are 8 hours apart.

In fact, it is easy to understand why the data we see is less than 8 hours, but how can we see the actual time?

There is a preferences under tools;


Select views, you can see that we are choosing UTC at this time;

At this point we need to select the above, using the local time zone to display:

At this point we can see in this client tool that the time we insert is consistent. Do you get it? ^_^


Note: Respect the original, if necessary reprint please indicate the source, thank you.

Is there a difference of 8 hours between the time of insertion and the actual time found in Mongovue?

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.