MongoDB C# 驱动程序和 DateTime 字段
MongoDB C# driver and DateTime field
我正在使用 C# 驱动程序将文档插入 MongoDB 集合,当我调试应用程序时,其中一种字段类型与 DateTime 相比,我在 "FrameTimeStamp" 字段中看到了服务器时间我传递给 Mongo,这是我的代码:
FrameDocument frameDoc = new FrameDocument();
frameDoc.Frame = imageBA;
frameDoc.EventCodeId = 1;
frameDoc.SesionId = 1;
frameDoc.FrameTimeStamp = DateTime.Now;
frameDoc.ServerUserId = (int)toMongoDt.Rows[0]["ServerUserId"];
frameDoc.TraderId = (int)toMongoDt.Rows[0]["TraderId"];
frameDoc.ActivePick = (int)toMongoDt.Rows[0]["ActivePick"];
frameDoc.TraderName = (string)toMongoDt.Rows[0]["TraderName"];
frameDoc.ServerUserName = (string)toMongoDt.Rows[0]["ServerUserName"];
var mongoCon = "mongodb://127.0.0.1";
MongoClient client = new MongoClient(mongoCon);
var db = client.GetDatabase("Video");
var frameCollection = db.GetCollection<FrameDocument>("Frame");
frameCollection.InsertOne(frameDoc);
在 shell 中,当我读取数据时,我看到的格式如下:
2016-08-14T06:10:33.295Z 并且时间不是服务器时间,它是 UTC,我如何强制 mongo 在传递 DateTime 时写入它, ?
MongoDB stores all DateTimes in UTC. Any local times you supply are
converted to UTC when stored in the database. The recommended approach
is to always convert DateTime values to UTC yourself before storing
them in the database, that way you are in full control. You can also
tell the C# driver that you want to work in LocalTime, like this:
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime Date
{ get; set; }
我正在使用 C# 驱动程序将文档插入 MongoDB 集合,当我调试应用程序时,其中一种字段类型与 DateTime 相比,我在 "FrameTimeStamp" 字段中看到了服务器时间我传递给 Mongo,这是我的代码:
FrameDocument frameDoc = new FrameDocument();
frameDoc.Frame = imageBA;
frameDoc.EventCodeId = 1;
frameDoc.SesionId = 1;
frameDoc.FrameTimeStamp = DateTime.Now;
frameDoc.ServerUserId = (int)toMongoDt.Rows[0]["ServerUserId"];
frameDoc.TraderId = (int)toMongoDt.Rows[0]["TraderId"];
frameDoc.ActivePick = (int)toMongoDt.Rows[0]["ActivePick"];
frameDoc.TraderName = (string)toMongoDt.Rows[0]["TraderName"];
frameDoc.ServerUserName = (string)toMongoDt.Rows[0]["ServerUserName"];
var mongoCon = "mongodb://127.0.0.1";
MongoClient client = new MongoClient(mongoCon);
var db = client.GetDatabase("Video");
var frameCollection = db.GetCollection<FrameDocument>("Frame");
frameCollection.InsertOne(frameDoc);
在 shell 中,当我读取数据时,我看到的格式如下: 2016-08-14T06:10:33.295Z 并且时间不是服务器时间,它是 UTC,我如何强制 mongo 在传递 DateTime 时写入它, ?
MongoDB stores all DateTimes in UTC. Any local times you supply are converted to UTC when stored in the database. The recommended approach is to always convert DateTime values to UTC yourself before storing them in the database, that way you are in full control. You can also tell the C# driver that you want to work in LocalTime, like this:
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
public DateTime Date
{ get; set; }