CosmosDb Mongo 数据库请求问题

CosmosDb Mongo database request issue

我有一个与 CosmosDb 上的 mongoDb 数据库交互的 .Net 应用程序。

我有一个 find 看起来像这样的请求:

public async Task<int?> GetCurrentZonierVersionNumberAsync(string productCode, string risqueType)
{
    if (string.IsNullOrEmpty(productCode) || string.IsNullOrEmpty(risqueType))
    {
        throw new ArgumentNullException();
    }

    var filter = Builders<Zonier>.Filter.And(
        Builders<Zonier>.Filter.Eq(x => x.ProductCode, productCode),
        Builders<Zonier>.Filter.Eq(c => c.RisqueType, risqueType));
    Zonier result = null;

    try
    {
        var query = _collection.Find(filter).SortByDescending(x => x.Version).Limit(1).FirstOrDefaultAsync();

        await _retryPolicy.ExecuteAsync(async () =>
        {
            result = await query;
        });
    }
    catch (TimeoutException ex)
    {
        throw new DataBaseReadingException(ex.Message, ExceptionCodeConstants.DataBaseReadingExceptionCode);
    }


    return result?.Version;
}

不幸的是,我在 azure 上收到以下错误消息:

Unhandled Exception: MongoDB.Driver.MongoCommandException: Command find failed: The provided cross partition query can not be directly served by the gateway. This is a first chance (internal) exception that all newer clients will know how to handle gracefully. This exception is traced, but unless you see it bubble up as an exception (which only happens on older SDK clients), then you can safely ignore this message.

我不明白发生了什么。有人可以向我解释错误信息吗? 我有 2.4.4 版本的 mongo 驱动程序。我知道我应该更新它,但我担心它会隐藏问题而不解决它。 我应该在 Polly 的重试策略中添加这种类型的异常吗?

由于我们已经确定您的 Cosmos DB 有数百万条记录,并且当前在 _id(您未包含在查询中的字段)上进行分区,因此这将导致 巨大cross-partition查询.

我不熟悉这个错误,但是,它可能是一个 resource-related 错误,因为查询太大而无法执行?您可以排除这种情况的唯一方法是执行包含 PK 的查询或 re-model 您的数据库使用更适合您正在执行的查询类型的 PK。

此外,如果您对错误本身进行一些深入研究,就会发现在两个不同的情况下,错误本身实际上是错误的结果:

非常值得研究您的工具集并确保您的 SDK 版本等都是最新的。