Using aggregation in MongoDB to select two or more fields of a document
I'm starting to work with MongoDB and I've a question about aggregation.
I've a document that use a lot of different fields in different orders.
For example:
db.my_collection.insert({ "answers" : [ { "id" : "0", "type" : "text",
"value" : "A"}, { "id" : "1", "type" : "text", "value" : "B"}]})
db.my_collection.insert({ "answers" : [ { "id" : "0", "type" : "text",
"value" : "C"}, { "id" : "1", "type" : "text", "value" : "A"}]})
I would to execute a query using "answers.id" with "answers.value" to
obtain a result.
I tried but didn't get results, in my case, I executed the command:
db.my_collection.aggregate({$match: {"answers.id":"0", "answers.value":
"A"}})
And the result was the two responses when I expected only:
{ "answers" : [ { "id" : "0", "type" : "text", "value" : "A"}, { "id" :
"1", "type" : "text", "value" : "B"}]
Can anyone help me please?
Thank you!!!
No comments:
Post a Comment