I'm new to MongoDB. I'm trying to get a person's name who has max age. Here is a sample of my document:
{
"_id": ....
"name": Alex,
"age": 23,
"city": NYC,
"isActive": False
}
{
"_id": .....
"name": Kate,
"age": 25,
"city": SFO,
"isActive": True
}
{
"_id": .....
"name": Dane,
"age": 31,
"city": SFO,
"isActive": null
}
Here is what I have tried so far
db.person.aggregate([{$group: {_id: {name: "$name", age: "$age"}}}, {$group: {_id: "", age: {$max: "$_id.age"}}}])
but I get only the age in the final output.
{ "_id" : "", "age" : 31 }
Is there a way to add the name is second pipe?