通过 MongoDB C# 驱动程序的多个过滤器

Multiple filters via MongoDB C# driver

运行时如何使用 official drivers 生成过滤器?我想要获取包含特定用户 ID 并且可以针对某些属性(包含一些文本)进行过滤的联系人

当前代码不适用于 request.Filter > 1:

private FilterDefinition<Contact> BuildFilter(NgTableRequest request, string userId)
{
    var filters = new List<FilterDefinition<Contact>>
    {
        Builders<Contact>.Filter.Where(q => q.ContactUsers.Any(w => w.UserId == userId))
    };

    if (request.Filter != null && request.Filter.Any())
    {
        foreach (var reqFilter in request.Filter)
        {
            filters.Add(Builders<Contact>.Filter.Regex(reqFilter.Key, reqFilter.Value[0]));
        }
    }

    var result = Builders<Contact>.Filter.And(filters);

    return result;
}

使用以下代码检查生成的过滤器,发现另一个地方有错误

    var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<Contact>();
    var renderedFilter = result.Render(documentSerializer, BsonSerializer.SerializerRegistry).ToString();
    Trace.WriteLine("Filter: " + renderedFilter);