I have an ASP.NET api to handle data going to a Mongo database. I need to also send some dynamic / irregular data for a number of documents, that’ll have a couple of extra fields.
So basically I’m trying to recreate the code from this tutorial
https://mongodb.github.io/mongo-csharp-driver/2.8/examples/mixing_static_and_dynamic/
but when i try to post or get the app will freeze, no errors or crashes though.
Swagger screenshot included, this is how it freezes and the only way to unfreeze is to restart the app from VS.
The class code:
public class Incident
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }
[BsonElement("Name")]
public string? Name { get; set; }
[BsonExtraElements]
public BsonDocument? Additional { get; set; }
}
It works just fine with static fields, but due to the nature of the data the db will receive I need some dynamic fields as well. Alternatively, is there any other way of achieving this?
2
Answers
Running this via postman gave me an error code:
I'll mark this as solved as this is what my original question was trying to find.
In general, I think you enable Swagger, Swagger translate and generate the whole
BsonDocument
object(too heavy), to avoid this hang and accept to pass dynamic params to object(document) byMongoDB.Driver
follow:System.Text.Json
enabled as defaultDictionary<string, object>
when Desterilize with object value or any type of object will beJsonElement
Microsoft Document and it’s hard to handle all props and passing to insert byMongoDB.Driver
, we should serialize orToString
then pars toBsonDocument
Example