Suppose I have a collection of survey response documents that look like this:
{
_id: ...
surveryId: ...
created: ...
answers: [
{
"k" : "53ac9a031703670858aff600",
"v" : "Yes"
},
{
"k" : "53b1e8961703670aa858e4ec",
"v" : "75731431"
},
...
{
"k" : "53b1e8961703670aa858e4ee",
"v" : "71825"
}
}
How would I create aggregation match operator where "53ac9a031703670858aff600" equals "Yes" and "53b1e8961703670aa858e4ec" equals "75731431" using the C# driver?
I can run the following query in mongo shell and get the results I am looking for:
db.Records.aggregate([{ "$match" : {
"answers" : { "$elemMatch" : { "k" : "53ac9a031703670858aff600", "v" : "Yes" } },
"answers" : { "$elemMatch" : { "k" : "53b1e8961703670aa858e4ec", "v" : "75731431" } }
}}])
I try to express the same query in C# but get an error because of the duplicate answer key:
new BsonDocument("$match", new BsonDocument
{
{ "answers", new BsonDocument
{
{
"$elemMatch", new BsonDocument
{
{ "k", "53ac9a031703670858aff600" },
{ "v", "Yes" }
}
}
}
},
{ "answers", new BsonDocument
{
{
"$elemMatch", new BsonDocument
{
{ "k", "53b1e8961703670aa858e4ec" },
{ "v", "75731431" }
}
}
}
}
}
I have tried using a single answers element with a BsonArray of $elemMatch elements, but the resulting JSON does not return any results.
How can I express the above $match operator using the C# driver?