问题
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.
回答1:
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.
回答2:
Query.ElemMatch("Field", new QueryDocument("$in", new BsonArray(new[] { "Hamster" })));
来源:https://stackoverflow.com/questions/22176263/using-both-in-and-elemmatch-using-the-c-sharp-driver