I have a event document with an array of ten people in it, each one with one score, i want to return the same event but with the people sorted by their score.
The json looks something like this:
"events": [
{
"_id": "622a2663658a760100143a4a",
"place": "Belfast",
"year": 2022,
"people": [
{
"name": {
"first": "Merita",
"last": "Giraud"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/31.jpg"
},
"gender": "female",
"email": "merita.giraud@example.com",
"age": 27,
"score": 9.174597203460992,
"_id": "622a2663658a760100143a4b"
},
{
"name": {
"first": "Yolanda",
"last": "Thomas"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/1.jpg"
},
"gender": "female",
"email": "yolanda.thomas@example.com",
"age": 50,
"score": 9.014386121808409,
"_id": "622a2663658a760100143a4c"
},
{
"name": {
"first": "Jorn",
"last": "Hakkens"
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/49.jpg"
},
"gender": "male",
"email": "jorn.hakkens@example.com",
"age": 47,
"score": 8.453602526940033,
"_id": "622a2663658a760100143a4d"
},
{
"name": {
"first": "Nils",
"last": "Adam"
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/9.jpg"
},
"gender": "male",
"email": "nils.adam@example.com",
"age": 53,
"score": 7.405307108969225,
"_id": "622a2663658a760100143a4e"
},
{
"name": {
"first": "Ceyhan",
"last": "Karaduman"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/61.jpg"
},
"gender": "female",
"email": "ceyhan.karaduman@example.com",
"age": 53,
"score": 9.79924527946733,
"_id": "622a2663658a760100143a4f"
},
{
"name": {
"first": "Sam",
"last": "Mendoza"
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/29.jpg"
},
"gender": "male",
"email": "sam.mendoza@example.com",
"age": 50,
"score": 8.378680917061923,
"_id": "622a2663658a760100143a50"
},
{
"name": {
"first": "Phyllis",
"last": "Pena"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/25.jpg"
},
"gender": "female",
"email": "phyllis.pena@example.com",
"age": 35,
"score": 8.463788241672331,
"_id": "622a2663658a760100143a51"
},
{
"name": {
"first": "Jamilla",
"last": "Curiel"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/66.jpg"
},
"gender": "female",
"email": "jamilla.curiel@example.com",
"age": 27,
"score": 8.595350377144374,
"_id": "622a2663658a760100143a52"
},
{
"name": {
"first": "Natali",
"last": "Hoyer"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/17.jpg"
},
"gender": "female",
"email": "natali.hoyer@example.com",
"age": 62,
"score": 7.800214827853902,
"_id": "622a2663658a760100143a53"
},
{
"name": {
"first": "Malou",
"last": "Sørensen"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/42.jpg"
},
"gender": "female",
"email": "malou.sorensen@example.com",
"age": 67,
"score": 9.793432061576496,
"_id": "622a2663658a760100143a54"
}
],
"__v": 0
}
]
}
My normal find to return the normal non sorted list looks like this:
const Event = require("../models/event");
const getEvents = async (req, res) => {
let result = Event.find();
const events = await result;
res.status(200).json({ events });
};
module.exports = {
getEvents,
};
I looked up to some solutions already here but couldn't find an appropriate solution. Like this one: Post