MongoDB data Query

I extract data from mongoDB successfully as List<mongobisondocument. the dataset some if empty values that causes error when write in to the datatable (Using add data column)

Please assist on how to replace empty values in List<mongobisondocument with any other value

Hi @ibraalli

You can loop through the list and check each document for empty values in its properties. If an empty value is found, you can replace it with a default value of your choice.

Please try implementing the following code snippet & let us know:

List<MongoDB.Bson.BsonDocument> documents = GetDocumentsFromMongoDB();
string defaultValue = "N/A";

foreach (var document in documents)
{
    foreach (var element in document.Elements)
    {
        if (element.Value == MongoDB.Bson.BsonNull.Value || element.Value == "")
        {
            document.Set(element.Name, defaultValue);
        }
    }
}

Hope this helps,
Best Regards.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.