2

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

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 上放了一个最小的示例。我的基础对象如下:

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 类中的每个字段,则输出有效。

额外的细节。.net 6 OSx MongoDB 云

4

1 回答 1

2

你想返回什么?如果你改变你的“去”路线到这个

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

你可能不会得到错误。FindAsync 返回一些 IEnumerable,因此您必须从中获取列表。

于 2021-12-28T20:42:25.607 回答