MongoDB 使用 .net 6 和 BsonDocument 字段类型

MongoDB with .net 6 and BsonDocument fieldtype

我正在尝试从我的 MongoDB 数据库中检索对象列表。我必须使用 BsonDocument 在记录上存储额外的动态数据,但是当我尝试使用最小 API return 列表到浏览器时,我收到以下错误。

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)

我已经尝试在对象上使用 BsonExtraElements 属性(如 docs)并得到相同的结果。

我在 Github here 上放了一个最小样本。我的基础对象如下所示:

public class BaseCollection
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId Id { get; set; }

    public BsonDocument Metadata { get; set; }
}

我在文档上的唯一记录是:

{"_id":{"$oid":"61cb03e65b7cd42eedf7a36b"},"Metadata":{"name":"Mauricio","lastname":"Sipmann"}}

我正在查询以下内容

context.GetCollection("teste").Find(new BsonDocument()).ToList<BaseCollection>()

如果我描述 BaseCollection class 中的每个字段,输出有效。

其他详细信息。 .net 6 OSx MongoDB云

你想要什么return?如果您将“前往”路线更改为此

app.MapGet("/go", async (IMongoDbContext context) => {
    var result = await context.GetCollection("teste").FindAsync(new BsonDocument());
    var list = await result.ToListAsync();
    return list.ToJson();
});

您可能不会收到错误消息。 FindAsync return 有一些 IEnumerable,因此您必须从中获取列表。