首页 > 解决方案 > 无法将“MongoDB.Bson.BsonString”类型的对象转换为“MongoDB.Bson.BsonBoolean”类型

问题描述

我不明白是什么导致了查询在 MongoDB shell 中正常工作的问题

public IEnumerable<BsonDocument> GetExams()
        {
            // db.exams.aggregate([
            // {$unwind:"$exams"},
            // {$project : {provider:1,exams:{ name:1,count: {$size :"$exams.questions" }} } },
            // {$group: {_id:"$provider",exams:{$push:"$exams"}} },
            // {$project: {_id:0,provider:"$_id",exams:"$exams"} }
            // ])

            return _examsCollection.Aggregate()
                .Unwind("exams")
                .Project(new BsonDocument
                {
                    {"provider", 1},
                    {
                        "exams",
                        new BsonDocument {{"name", 1}, {"count", new BsonDocument {{"$size", "$exams.questions"}}}}
                    }
                })
                .Group(new BsonDocument {{"_id", "$provider"}, {"exams", new BsonDocument {{"$push", "$exams"}}}})
                .Project(new BsonDocument {{"_id", 0}, {"provider", "$_id"}, {"exams", "$exams"}})
                .ToList();
}

这是错误代码

System.InvalidCastException:无法将“MongoDB.Bson.BsonString”类型的对象转换为“MongoDB.Bson.BsonBoolean”类型。在 System.Text.Json.JsonPropertyInfo.Write(WriteStack& state, Utf8JsonWriter writer) 的 System.Text.Json.JsonPropertyInfoNotNullable`4.OnWrite(WriteStackFrame& current, Utf8JsonWriter writer) 的 get_AsBoolean(Object)。在 Microsoft.AspNetCore.Mvc 的 System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancelToken) 处写入(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)。 Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext 上下文,

标签: c#.netmongodbasp.net-core

解决方案


推荐阅读