Show
// Insert records
db.engineers.insertOne(
{
"team":
[
{
"name": "pat",
"age": 30,
"address": { "street": "12 Baker St", "city": "London" }
},
{
"name": "dallas",
"age": 36,
"address": { "street": "12 Cowper St", "city": "Palo Alto" }
},
{
"name": "charlie",
"age": 42,
"address": { "street": "12 French St", "city": "New Brunswick" }
},
{
"name": "Dallas",
"age": 36,
"address": { "street": "12 Cowper St", "city": "Palo Alto" }
}
]
})
sort on a field:
db.engineers.aggregate( [
{ $project:
{
_id: 0,
result:
{
$sortArray: { input: "$team", sortBy: { name: 1 } }
}
}
}
] )
// Result of the above query
[
{
"result": [
{
"address": {
"city": "Palo Alto ",
"street": "12 Cowper St"
},
"age": 36,
"name": "Dallas"
},
{
"address": {
"city": "New Brunswick",
"street": "12 French St"
},
"age": 42,
"name": "charlie"
},
{
"address": {
"city": "Palo Alto",
"street": "12 Cowper St"
},
"age": 36,
"name": "dallas"
},
{
"address": {
"city": "London",
"street": "12 Baker St"
},
"age": 30,
"name": "pat"
}
]
}
]