使用C#驱动程序同时使用$ in和$ elemMatch [英] Using both $in and $elemMatch using the C# Driver
本文介绍了使用C#驱动程序同时使用$ in和$ elemMatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难将有效的MongoDB
查询转换为C#驱动程序的无类型等效项.
查询:
I'm having a hard time translating a working MongoDB
query to the C# driver's untyped equivalent.
The query:
{
"Field" : { "$elemMatch" : { "$in" : ["Hamster"]}}
}
我所拥有的:
Query.ElemMatch("Field", Query.In("", new BsonArray(new[] { "Hamster" })));
哪个生成:
{
"Field" : { "$elemMatch" : { "" : { "$in" : ["Hamster"] }}}
}
那非常接近,但是我不知道如何从$ in查询中删除该名称.
That's pretty close but i can't figure out how to remove the name from the $in query.
推荐答案
似乎应该可行,但是辅助方法的结构方式让我看不到直接构造该查询的简便方法.
It seems like it should be possible, but the way the helper methods are structured I can't see an easy way of constructing that query directly.
我可以重新创建所需查询的唯一方法是执行以下操作:
The only way I could recreate the desired query is by doing variations of the following:
var queryDocument = QueryDocument.Parse("{\"$in\" : [\"Hamster\"]}");
var nestedQueryDocument = Query.ElemMatch("Field", new QueryDocument(queryDocument));
创建以下输出
{
"Field" : { "$elemMatch" : { "$in" : ["Hamster"]}}
}
虽然不是最优雅的解决方案.
Not the most elegant of solutions though.
这篇关于使用C#驱动程序同时使用$ in和$ elemMatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文