skip to Main Content

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


  1. Chosen as BEST ANSWER

    Running this via postman gave me an error code:

    Unable to cast object of type 'MongoDB.Bson.BsonString' to type 'MongoDB.Bson.BsonBoolean'.

    I'll mark this as solved as this is what my original question was trying to find.


  2. 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) by MongoDB.Driver follow:

    • first if you are using dotnet core, System.Text.Json enabled as default
    • Dictionary<string, object> when Desterilize with object value or any type of object will be JsonElement Microsoft Document and it’s hard to handle all props and passing to insert by MongoDB.Driver, we should serialize or ToString then pars to BsonDocument

    Example

     //pass strong type
         public class Foo
        {
    
            [BsonId]
            [BsonRepresentation(BsonType.ObjectId)]
            public string? Id { get; set; }
    
            [Required]
            public string Name { get; set; }
          
    
            [BsonExtraElements]
            public Dictionary<string, object> Metadata { get; set; }
    
            public void TranslateMetadata()
            {
                if (this.Metadata is null) return;
                var json = JsonSerializer.Serialize(this.Metadata);
                this.Metadata = BsonDocument.Parse(json).ToDictionary();
            }
        }
    
    // Add
    //case 1 :
     public void Add(Foo foo) {
                foo.TranslateMetadata();
                await Context1.InsertOneAsync(foo);
      }
    
    
    
    
    //case2 :  pass object
    
        public void Add(object foo)  //object from foo
        { 
                var json = foo.ToString();
                this.Metadata = BsonDocument.Parse(json);
    
                await Context1.InsertOneAsync(foo);
         }
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search