Elfocrash / Cosmonaut

🌐 A supercharged Azure CosmosDB .NET SDK with ORM support
https://cosmonaut.readthedocs.io
MIT License
341 stars 44 forks source link

ISO formatted date getting converted when querying #73

Closed StickNitro closed 5 years ago

StickNitro commented 5 years ago

I have a CosmosDB collection where I am storing ISO formatted dates as strings, for example, I have saved a document with a start value of 2018-07-26T09:00:00Z and when I look in the Data Explorer I can see the value saved correctly.

I am using Cosmonaut to access my CosmosDB which exposes the DocumentClient and I am using CreateDocumentQuery to access my document but when the document gets returned from CosmosDB the start value is converted to a DateTime string 07/26/2018 09:00:00

var collectionUri = UriFactory.CreateDocumentCollectionUri("assessment-center", "ac-template-timetables");
Document document = await _cosmonautClient.DocumentClient.CreateDocumentQuery(collectionUri, new SqlQuerySpec
{
    QueryText = "SELECT * FROM c WHERE c.id = '713d5115-83cb-4005-abaa-25756ebc86d1'"
}, new FeedOptions { EnableCrossPartitionQuery = true }).FirstOrDefaultAsync();

The above is my query, has anyone else encountered this and if so how did they resolve this problem, my expectation is that my string value should be returned as is and not interpreted as a DateTime which seems to be what is happening

Elfocrash commented 5 years ago

This sounds like an issue with the underlying CosmosDB SDK, not with Cosmonaut. You can easily resolve this by specifying a converter. Here is a stackoverlfow question with relevant answers: https://stackoverflow.com/questions/18635599/specifying-a-custom-datetime-format-when-serializing-with-json-net

Let me know if that helped.