Correct insert DateTime from c# to mongodb

后端 未结 4 764
情歌与酒
情歌与酒 2020-12-29 13:52

I try to insert local time in MongoDB

var time = DateTime.Now; // 03.05.2014 18:30:30

var query = new QueryDocument
{
   { \"time\", nowTime}
};

collection         


        
4条回答
  •  [愿得一人]
    2020-12-29 14:53

    The MongoDB driver is converting your DateTime to UTC.

    If you don't want to bother with time zones, you could change the kind to UTC:

    time = DateTime.SpecifyKind(time, DateTimeKind.Utc);
    

    I would not recommed this because the database will store a "wrong" Date, but it will show you ISODate("2014-05-03T18:30:30.170Z").

提交回复
热议问题