I'm trying to retrieve a list of objects from my MongoDB Database. I have to use a BsonDocument to store additional dynamic data on the record, but when I try to return a list to the browser using minimal API I get the following error.
InvalidCastException: Unable to cast object of type 'MongoDB.Bson.BsonString' to type 'MongoDB.Bson.BsonBoolean'.
AsBooleanGetter(object )
System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter<TCollection, TElement>.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonCollectionConverter<TCollection, TElement>.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer)
System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.Converters.ListOfTConverter<TCollection, TElement>.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonCollectionConverter<TCollection, TElement>.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.TryWrite(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.WriteCore(Utf8JsonWriter writer, ref T value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.Serialization.JsonConverter<T>.WriteCoreAsObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.JsonSerializer.WriteCore<TValue>(JsonConverter jsonConverter, Utf8JsonWriter writer, ref TValue value, JsonSerializerOptions options, ref WriteStack state)
System.Text.Json.JsonSerializer.WriteStreamAsync<TValue>(Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
System.Text.Json.JsonSerializer.WriteStreamAsync<TValue>(Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
System.Text.Json.JsonSerializer.WriteStreamAsync<TValue>(Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsyncSlow<TValue>(Stream body, TValue value, JsonSerializerOptions options, CancellationToken cancellationToken)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
I have already tried using BsonExtraElements attribute on the object (like the docs) and got the same result.
I've put a minimal sample on Github here. My base object is like the following:
public class BaseCollection
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }
public BsonDocument Metadata { get; set; }
}
My only record on the document is:
{"_id":{"$oid":"61cb03e65b7cd42eedf7a36b"},"Metadata":{"name":"Mauricio","lastname":"Sipmann"}}
I'm queriyng with the following
context.GetCollection("teste").Find(new BsonDocument()).ToList<BaseCollection>()
If I describe every field inside the BaseCollection class, the output works.
Aditional details. .net 6 OSx MongoDB Cloud
CodePudding user response:
What do you want to return? if you change your "go" route to this
app.MapGet("/go", async (IMongoDbContext context) => {
var result = await context.GetCollection("teste").FindAsync(new BsonDocument());
var list = await result.ToListAsync();
return list.ToJson();
});
You probably won't get the error. FindAsync returns some IEnumerable, so you have to get the list from it.