Extra Elements Stored as Empty Arrays

I am working on creating a C# .NET application that will be storing data in Mongo. This application will be storing documents that have extra elements that are complex objects, and I have added [BsonExtraElements] to the Dictionary that contains the extra elements. However, what is actually being stored is not what I am expecting. I can see the top level fields of the extra data but then there are just a lot of empty arrays.

The extra data being passed in:

    {
    "Suppression": {
      "Id": 0,
      "Name": "string",
      "Description": "string",
      "AssetKey": "string",
      "Prod": true,
      "StartTime": "2020-11-05T22:16:32.452Z",
      "EndTime": "2020-11-05T22:16:32.452Z",
      "EnteredBy": "string",
      "EnteredTime": "2020-11-05T22:16:32.452Z",
      "Status": true,
      "PlannedMaintenance": true
    },
    "Filters": [
      {
        "Type": "string",
        "Value": "string"
      }
    ]
  }

Compared with how these are stored in the database

    "Suppression": [
        [
            []
        ],
        [
            []
        ],
        [
            []
        ],
        [
            []
        ],
        [
            []
        ],
        [
            []
        ],
        [
            []
        ],
        [
            []
        ],
        [
            []
        ],
        [
            []
        ],
        [
            []
        ]
    ],
    "Filters": [
        [
            [
                []
            ],
            [
                []
            ]
        ]
    ]

If you make a simple example that fails you as above (and describe also what you expected to see!) surely some C#.NET expert will come along and correct your code.

1 Like

Here is some simplified code of what is in the application.

This is the class that I am trying to store in the database:

    public class Schedule
        {
            public string Id { get; set; }
            [JsonProperty(Required = Required.Always)]
            public string Asset { get; set; }
            public string NamedEnvironment { get; set; }
            [JsonProperty(Required = Required.Always)]
            public string Name { get; set; }
            [JsonProperty(Required = Required.Always)]
            public ScheduleType Type { get; set; }
            public string Description { get; set; }
            [JsonProperty(Required = Required.Always)]
            public DateTime StartTime { get; set; }
            [JsonProperty(Required = Required.Always)]
            public DateTime EndTime { get; set; }
            [BsonExtraElements]
            public IDictionary<string, object> Data { get; set; }
        }

The Data dictionary exists to hold additional dynamic properties.
The code to add this to the database is very simple

     public Schedule AddSchedule(Schedule s)
            {
                _scheduleCollection.InsertOne(s);
                return s;
            }

Then here is the full object being passed in, and what I am expecting to be stored in the database

    {
      "asset": "Example",
      "name": "Example",
      "type": "Suppression",
      "description": "Example",
      "startTime": "2020-11-06T14:49:36.779Z",
      "endTime": "2020-11-06T14:49:36.779Z",
      "data": {
        "Suppression": {
          "Id": 0,
          "Name": "string",
          "Description": "string",
          "AssetKey": "string",
          "Prod": true,
          "StartTime": "2020-11-06T14:50:09.494Z",
          "EndTime": "2020-11-06T14:50:09.494Z",
          "EnteredBy": "string",
          "EnteredTime": "2020-11-06T14:50:09.494Z",
          "Status": true,
          "PlannedMaintenance": true
        },
        "Filters": [
          {
            "Type": "string",
            "Value": "string"
          }
        ]
      }
    }

But when I try to retrieve this what I get back is

    {
        "id": "5fa5631d5b2a8f5d2f9cdecd",
        "asset": "Example",
        "namedEnvironment": null,
        "name": "Example",
        "type": "Suppression",
        "description": "Example",
        "startTime": "2020-11-06T14:49:36.779Z",
        "endTime": "2020-11-06T14:49:36.779Z",
        "data": {
          "suppression": [
            [
              []
            ],
            [
              []
            ],
            [
              []
            ],
            [
              []
            ],
            [
              []
            ],
            [
              []
            ],
            [
              []
            ],
            [
              []
            ],
            [
              []
            ],
            [
              []
            ],
            [
              []
            ]
          ],
          "filters": [
            [
              [
                []
              ],
              [
                []
              ]
            ]
          ]
        }
      }

is not suppression … case sensitivity.

It is not, we have an API object which has the JSON property names set to lowercase, but this will map to the uppercase C# property names.

I was able to come up with a solution today. The API object will accept a dictionary as it already is, however in the Schedule object I have changed the Data property to a BsonDocument. During the mapping of the API object to a Schedule, I convert the Dictionary to a JSON string, then parse this JSON to a BsonDocument. This stores the data correctly, then we just reverse the process when retrieving the data.

1 Like

Yes, I encounter the same sort of thing in PHP where sometimes I want to preserve the BSON objects into the PHP objects and sometimes I want to convert in and out. Good work.