Insert Time:
Db.test.insert ({time:new Date ()})
When data was inserted into the date format for MongoDB, it was found that the date time was 8 hours apart, the time originally stored in MongoDB was the standard time UTC +0:00, while China's time zone was +8.00.
correct when removed
Db.test.find () [0].time.gethours ()
It is therefore necessary to process the time when inserting:
Db.test.insert ({"Time":new date ( new Date (). getFullYear () + "-" + (new date (). GetMonth () +1) + "-" +new date (). GetDate () + "+new date (). toLocaleTimeString ())}) Db.test.insert ({' time ' : Isodate ("2012-11-02 07:58:51")}) Db.user.uinfo.insert ({' ltime ': Isodate ("2012-11-02 07:58:51")})
With custom functions:
functionInsertdate (Time) {time.sethours (time.gethours ()-8); returnTime ;} Insertdate (NewDate (2017,9,9,9,63,72))functionGetformatdate (time) { year=time.getfullyear (); Mon= Time.getmonth () +1; Date=time.getdate (); Hour=time.gethours (); Min=time.getminutes (); SEC=time.getseconds (); NewTime= year+ '-' +mon+ '-' +date+ ' +hour+ ': ' +min+ ': ' +sec; returnNewTime;}
MongoDB insertion Time