Franck
Franck

Reputation: 4440

Creating a LocalTime BsonDatetime object

I have a large logging and data entry program that receives DataTable object with 1 DataRow. The DataTable have random amount of columns, with random column name and type so i cannot have class for each. Using these DataColumn i get the DataType and build a BsonDocument from scratch out of this.

Here's a short example

public void ParseData(DataTable table)
{
    // create the document
    var document = new BsonDocument();

    // get the only row in the table
    var row = table.Rows[0];
            
    // for each column we add the property
    foreach (DataColumn column in table.Columns)
    {
        // create an empty value
        BsonValue value = null;

        // current column value
        var columnValue = row[column.ColumnName];

        // set the value based on the datatype
        if (column.DataType == typeof(string)) value = new BsonString(columnValue.ToString());
        else if (column.DataType == typeof(int)) value = new BsonInt32(Convert.ToInt32(columnValue));
        else if (column.DataType == typeof(float)) value = new BsonDouble(Convert.ToDouble(columnValue));
        else if (column.DataType == typeof(double)) value = new BsonDouble(Convert.ToDouble(columnValue));
        else if (column.DataType == typeof(bool)) value = new BsonBoolean(Convert.ToBoolean(columnValue));
        else if (column.DataType == typeof(DateTime)) value = new BsonDateTime(Convert.ToDateTime(columnValue));

        // add the element
        document.Add(new BsonElement(column.ColumnName, value));
    }

    // insert the document in the generic collection
    InsertDocument(document);
}

As you can see it's pretty simple. I have removed a lot of types in the list as we have many custom types that might pass so i just kept the basic ones. The problem is that i cannot figure out how to force the BsonDateTime to save as local time in the collection. When doing filters with legacy apps it's not working. I need them to be saved as local time. It's never been an issue in the past but because of those legacy apps from the early 90's that still need support i have to figure something out.

I also need to reload them as local time. If i could, i would save them as string but i can't because since all columns are random i do not know when loading if a specific BsonString is really a string or if it's a DateTime. For reloading i must not reload really as local time. I must reload the exact value in the database. I only control the creation of the document. But reading i only control a few one's that will be reading from it that are in C#, Java and C++. The rest are legacy apps that companies doesn't even exist anymore.

I did try to just modify every single date that came in the system to account for UTC and change the date to when saved as UTC it's stored property and filters from legacy apps still works but all of .NET, Java and C++ apps load up the wrong value and not the written value.

Is there a way to just disable UTC in a specific collection or database in MongoDB directly like you can in SQL server ?

Upvotes: 0

Views: 1129

Answers (1)

D. SM
D. SM

Reputation: 14490

MongoDB stores times in UTC and does not have time zone support. You can store any values you like but they will be interpreted as UTC timestamps by most MongoDB-related software.

Upvotes: 1

Related Questions