Question

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.

Was it helpful?

Solution 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.

OTHER TIPS

Query.ElemMatch("Field", new QueryDocument("$in", new BsonArray(new[] { "Hamster" })));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top