c# - 错误:命令 getMore 失败。原因:(对象引用未设置为对象的实例。)
问题描述
我在使用排序查询 MongoDB 时遇到了这个问题。
MongoDB.Driver.MongoCommandException: Command getMore failed: [ActivityId=f840b650-9663-404e-9d74-ae5a4f02b06a] Error=1, Details='Response status code does not indicate success: InternalServerError (500); Substatus: 0; ActivityId: 00000000-0000-0000-0000-000000000000; Reason: (Object reference not set to an instance of an object.);.
at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ProcessResponse(ConnectionId connectionId, CommandMessage responseMessage)
at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol`1 protocol, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.AsyncCursor`1.ExecuteGetMoreCommandAsync(IChannelHandle channel, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.AsyncCursor`1.GetNextBatchAsync(CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.AsyncCursor`1.MoveNextAsync(CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorExtensions.ToListAsync[TDocument](IAsyncCursor`1 source, CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
我的代码中设置的排序列是UpdatedDate,DESC,数据库集合中缺少此列
var collections = _mongoDatabase.GetCollection<T>(typeof(T).Name.ToLower());
if (filterCondition == null)
{
filterCondition = Builders<T>.Filter.Empty;
}
return await collections
.Find(filterCondition ?? FilterDefinition<T>.Empty, new FindOptions() { Collation = new Collation("en", strength: CollationStrength.Secondary) })
.Sort(sortDefinition)
.ToListAsync();
但是,当我为所有记录的 UpdatedDate 字段设置值时,查询成功运行
为什么 MongoDB 不支持对缺失列进行排序?
更多信息:
实体类 A
class A {
public string Name {get;set;}//always having value
public string CreatedDate {get;set;} // always having value
public string UpdatedDate{get;set;} // missing in database
}
.NET 中的 MongoDB 驱动程序版本:2.9.3。Mongodb 版本:3.6.0,托管在 Azure(Azure Cosmos DB API for MongoDB)上。
解决方案
推荐阅读
- excel - 列范围可以与 SUBSTITUTE 一起使用吗
- python - 如何使用 PyCharm 激活我的虚拟环境?我什至可以在 PyCharm 中激活它吗?
- gridsome - 单击按钮后调用 API
- javascript - 如何添加 4 位小数的输出结果?
- flutter - 带有不匹配参数的闭包调用:函数'LoginScreen.build。
- bash - 如何使用现有文件夹名称作为基本名称创建按顺序编号的文件夹列表
- python - django:如何从 View.py 读取和操作 .csv 文件
- flutter - 如何在 ListView 中显示来自实时数据库的数据?
- automation - 有没有办法每 n 秒重新运行一个特定的 Jupyter Lab 单元?
- javascript - React Native 无法显示来自对象的图像