首页 > 解决方案 > 错误:命令 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)上。

标签: c#mongodbazure-cosmosdb-mongoapi

解决方案


推荐阅读