c# - 无法将“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 上下文,
解决方案
推荐阅读
- python - 显示两个数据框列中两个字符串之间的差异的位置,pandas
- javascript - Office.js onReady/initialize 在 Internet Explorer 上不起作用
- import - 无法打开文件“load_departments.dump”;错误:2
- javascript - 在Angular 2+中处理子组件中通过引用传递的对象
- hibernate - 如何在不触发验证的情况下将模型传递给表单(编辑前)
- javascript - 如何在 Google Sheets API (Cocos Creator) 中使用“查找”
- url - 亚马逊如何缩短会员网址
- c# - F#中如何调用按参数类型重载的方法?
- c# - 另一个控件问题的GotFocus事件期间WPF焦点DataGrid
- c# - 使用 AddForceAtPosition 对象仅移动但不扭转