✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
A collection RACE has been created in MongoDB. Some of the documents have been inserted using the following MongoDB shell command:
db.race.insertMany([
{"_id": 102,
"event": "Great Ocean Road Ultra 2017",
"distance": 60,
"runners": [{ "name": "Steve Smith", "gender": "M", "age": 45 },
{ "name": "Debbie Picket", "gender": "F", "age": 47 } ]},
{"_id": 103,
"event": "Great Ocean Road Maratahon 2017",
"distance": 42.2,
"runners": [{ "name": "Cathy Freeman ", "gender": "F", "age": 25 },
{ "name": "Sebastian Coe", "gender": "M", "age": 62 } ]
}])
The following queries are written to find the name of all the female runners older than 60 years old.
Approach A
db.race.aggregate(
{$unwind:"$runners"},
{$match:{"runners.gender":"F","runners.age":{$gt:60}}}
)
Approach B
db.race.aggregate(
{$match:{"runners":{$elemMatch:{"gender":"F","age":{$gt:60}}}}},
{$unwind:"$runners"},
{$match:{"runners.gender":"F","runners.age":{$gt:60}}}
)
Assume there are 100 documents in the collection and each document contains 1000 runners in the runners array. The number of documents (event document) containing female runners older than 60 is 15. How many documents will be in the results of the $unwind operation in Approach B?