简体   繁体   English

C#(和MongoDB)DateTime转换(从BSON到C#对象)

[英]C# (and MongoDB) DateTime conversion (from BSON to C# object)

I've been using MongoDB C# driver, transferring some documents from our current database to MongoDB and the other way round. 我一直在使用MongoDB C#驱动程序,将一些文档从当前数据库传输到MongoDB,反之亦然。 The problem I get is (in short): Although the method for retrieving DateTime from a MongoDB document returns the correct date/time, when assigned to a DateTime attribute of my C# object it reverts to 01/01/0001 12:00:00 AM, that is, to a min DateTime value. 我得到的问题是(简而言之):尽管从MongoDB文档中检索DateTime的方法返回了正确的日期/时间,但是当分配给我的C#对象的DateTime属性时,它将恢复为01/01/0001 12:00:00 AM,即最小DateTime值。

More details: 更多细节:

GetdateTime is querying MongoDB (rdoc is a BSON document) and returning the correct date/time: GetdateTime正在查询MongoDB(rdoc是BSON文档)并返回正确的日期/时间:

Console.WriteLine(GetDateTime(rdoc, "recordedDate"));

The output is: 输出为:

6/3/2010 10:00:00 AM 2010/6/3上午10:00:00

I assign the returned value to my c# object: 我将返回值分配给我的c#对象:

doc.RecordedDate = GetDateTime(rdoc, "recordedDate");

but when I access this attribute using (r is my c# object): 但是当我使用(r是我的c#对象)访问此属性时:

Console.WriteLine(r.UserID + "," + r.DateCreated);

I get: 123456,1/1/0001 12:00:00 AM 我得到:123456,1 / 1/0001 12:00:00 AM

I am setting my DateTime field with: 我用以下方法设置我的DateTime字段:

set{ recordedDate = value.ToLocalTime();  }

Just to mention here that I am not actually updating our database when creating c# docs, just keeping these docs into memory then resaving them to Mongo (don't ask), just for testing purposes. 这里只说我创建c#文档时实际上并没有更新我们的数据库,只是将这些文档保存在内存中,然后将它们重新保存到Mongo(不要问),只是为了测试。 It shouldn't affect the problem I am having, all other fields work fine. 它不应该影响我遇到的问题,所有其他字段都可以正常工作。

I know that there is some missmatch between DateTime objects of BSON and .NET (BSON truncates number of Ticks or something) but don't if this is causing my problem... 我知道BSON和.NET的DateTime对象之间存在某些不匹配(BSON会截断Ticks数或其他内容),但是如果这导致了我的问题,那是不会的...

Btw, I only started using c# three days ago, so if I am missing something basic and obvious here, please point it out gently :). 顺便说一句,我只是三天前才开始使用c#,因此,如果我在这里缺少一些基本且显而易见的内容,请轻轻指出:)。

You seem to have a mismatch between how you're storing your data. 您似乎在存储数据的方式上不匹配。

This is updating the property doc.RecordedDate : 这将更新属性doc.RecordedDate

doc.RecordedDate = GetDateTime(rdoc, "recordedDate");

But you are reading from the property DateCreated : 但是您正在从属性DateCreated中读取:

Console.WriteLine(r.UserID + "," + r.DateCreated)

And this is writing to a third variable 'recordedDate': 这正在写入第三个变量“ recordedDate”:

set{ recordedDate = value.ToLocalTime();  }

You're writing to a thing named RecordedDate, but reading from a thing named DateCreated. 您正在写一个名为RecordedDate的东西,但正在读取一个名为DateCreated的东西。 Where do you actually update DateCreated? 您实际上在哪里更新DateCreated? What is the relationship between doc.RecordedDate and r.DateCreated? doc.RecordedDate和r.DateCreated之间是什么关系? What is the relationship between recordedDate and doc.RecordedDate or r.DateCreated? recordedDate和doc.RecordedDate或r.DateCreated之间是什么关系?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM