I'm having a hard time translating a working MongoDB query to the C# driver's untyped equivalent. The query:

{
    "Field" : { "$elemMatch" : { "$in" : ["Hamster"]}}
}

What I have:

Query.ElemMatch("Field", Query.In("", new BsonArray(new[] { "Hamster" })));

Which generates:

{
    "Field" : { "$elemMatch" : { "" : { "$in" : ["Hamster"] }}}
}

That's pretty close but i can't figure out how to remove the name from the $in query.

有帮助吗?

解决方案 2

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));

Creates the following output

{
    "Field" : { "$elemMatch" : { "$in" : ["Hamster"]}}
}

Not the most elegant of solutions though.

其他提示

Query.ElemMatch("Field", new QueryDocument("$in", new BsonArray(new[] { "Hamster" })));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top