We are currently working on a .NET Core application and we are using MongoDB for it. I am using .NET Driver to access the data. All the data we are saving in a collection has different types of data structure.
For example, it has first document which has Name, Phone and a Payload which has embedded document in which we are saving address:
{ “Name”: “TestName”, “Phone”: “23846787”, “Payload”: { “Address”: “TestAddress”, “City”: “TestCity” }, “Active”: true }
Then in the same collection we have another document which has Name, Phone and a Payload which is completely different from first one:
{ “Name”: “TestName2”, “Phone”: “54568765”, “Payload”: { “Weight”: “70”, “Age”: “45”, “Gender”: “Female” } }
Now when we use .NET driver to get both of these records, we get an error because it cannot cast the embedded document into an object (as it doesnt know about the object). We need to tell it, which type of object is the embedded document. But we dont want to do it because we have several types of payload we want to save. I tried using discriminator “_t” but it didn’t help.
Can someone please suggest how we can read the data when we have different elements in the document and also has embedded documents ??
Thank you
JW