我'm writing an OData v4 service. I'm使用MongoDB作为我的后端 . 我可以使用 FilterDefinition<T> 轻松编写ElemMatch查询来解决我的问题:

var eqF = Builders<IndicesType>.Filter.Eq(x => x.Value, "Foo");
var elemF = Builders<DocumentType>.Filter.ElemMatch(_ => _.Indices, eqF);

我尝试使用 IMongoCollection#AsQueryable() 方法从我的存储库返回 IQueryableIMongoQueryable . 当我尝试在我的 endpoints 上使用以下OData过滤器时,C#驱动程序抛出InvalidOperationException:

$filter=Indices/any(i: i/Value eq 'Foo')

异常消息是

"IIF(({document}{i} == null), null, Convert(Any({document}{i}.Where((IIF(({document} == null), null, {document}{k}) == \"Foo\"))))) is not supported."

我认为这个操作将得到新驱动程序广泛的LINQ支持的支持 . 是否有实现此计划或我是否需要编写转换层以将OData查询转换为Mongo过滤器或BsonDocument并手动运行查询?

对于测试,将整个集合转换为IEnumerable可以进行适当的过滤,但是当集合大小增加时,此解决方案显然不适用于 生产环境 .

我还应该考虑其他解决方法吗?

Edit :在这里找到了JIRA票据https://jira.mongodb.org/browse/CSHARP-1771,如果有人倾向于创建驱动程序的自定义构建,它实际上有一个解决方法 .