gpt4 book ai didi

c# - MongoDB C# 驱动程序和 DateTime 字段

转载 作者:可可西里 更新时间:2023-11-01 09:18:48 25 4
gpt4 key购买 nike

我正在使用 C# 驱动程序将文档插入 MongoDB 集合,当我调试应用程序时,其中一种字段类型与 DateTime 相比,我在传递给 Mongo 的“FrameTimeStamp”字段中看到服务器时间,这是我的代码:

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 时写入它?

最佳答案

根据 CSHARP-185

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 C# 驱动程序和 DateTime 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39318196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com