I am sending the UTC Datetime from my C# winform app to MongoDb 3.4.5, but it is stored as local time in MongoDb. When I get the data from Mongo it returns me the UTC time.
My problem here is why the datetime is stored in local in MongoDb in-spite of sending UTC Datetime from UI. I want UTC time to be stored in MongoDb. Below is the code snippet:
public void InsertInMongo()
{
var client = new MongoClient("mongodb://localhost:27017");
var db = client.GetDatabase("Test");
var col = db.GetCollection<TestData>("TestData");
var data = new TestData(123,DateTime.UtcNow);
col.InsertOne(data);
var fromMongo = col.Find(Builders<TestData>.Filter.Empty).ToList();
}
}
class TestData
{
public TestData(int num, DateTime date)
{
TestNumber = num;
TestDate = date;
}
[BsonId]
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
[BsonIgnoreIfDefault]
public string Id { get; private set; }
public int TestNumber { get; set; }
public DateTime TestDate { get; set; }
}
}
How can I resolve this? I am newbie in Mongo with C#.