문제

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