[英]Get DateTime from mongo collection using c# driver
I have the following object: 我有以下对象:
public class QueueItem
{
public long _id { get; set; }
public SiteMapRequestState State { get; set; }
public int WorkerId { get; set; }
public DateTime QueuedTime { get; set; }
public DateTime StartWorkTime { get; set; }
}
When I save the object to mongo the date appear right (the DateTime fields contains the correct date that I inserted). 当我将对象保存到mongo时,日期显示为右(DateTime字段包含我插入的正确日期)。 However when i pull this object I get different time (-3 hours). 然而,当我拉这个物体时,我会得到不同的时间(-3小时)。
For example: I save the object with StartWorkTime = 6/26/2012 10:00:00 AM , then when getting the object from the collection StartWorkTime equals to 6/26/2012 7:00:00 AM . 例如:我使用StartWorkTime = 6/26/2012 10:00:00 AM保存对象,然后从集合中获取对象StartWorkTime等于6/26/2012 7:00:00 AM 。 This make the calculation of process time wrong. 这使得处理时间的计算错误。
I'm using the latest c# driver (1.4.2.4500) and running both code and the mongo server on the same machine. 我正在使用最新的c#驱动程序(1.4.2.4500)并在同一台机器上运行代码和mongo服务器。
What can cause the difference? 有什么可以导致差异?
Thanks! 谢谢!
After digging a bit i found this posts: http://alexmg.com/post/2011/09/30/DateTime-precision-with-MongoDB-and-the-C-Driver.aspx 挖了一下我发现这个帖子: http : //alexmg.com/post/2011/09/30/DateTime-precision-with-MongoDB-and-the-C-Driver.aspx
http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-DateTimeSerializationOptions http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-DateTimeSerializationOptions
I needed to mark the DateTime property with [BsonDateTimeOptions(Kind = DateTimeKind.Local)] attribute 我需要使用[BsonDateTimeOptions(Kind = DateTimeKind.Local)]属性标记DateTime属性
I had this issue once. 我曾经遇到过这个问题。 Be sure to also specify the DateTimeKind parameter to DateTimeKind.Utc 请务必同时为DateTimeKind.Utc指定DateTimeKind参数
Hope that helped. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.