Hello guys,
[
{
"_id" : "1",
"name" : "John",
"age" : "28",
"location" : {
"citiesLived":[
{"name":"London"},
{"name":"Toronto"},
{"name":"Paris"}
]
}
},
{
"_id" : "12",
"name" : "Jane",
"age" : "32",
"location" : {
"citiesLived":[
{"name":"Enugu"},
{"name":"Abuja"},
{"name":"Lagos"}
]
}
},
{
"_id" : "34",
"name" : "Ben",
"age" : "24",
"location" : {
"citiesLived":[
{"name":"New York"},
{"name":"Manchester"},
{"name":"Nova Scotia"}
]
}
},
{
"_id" : "08",
"name" : "Chris",
"age" : "24",
"location" : {
"citiesLived":[
{"name":"California"},
{"name":"Ontario"},
{"name":"Texas"}
]
}
}
]
Please how do I sort data of this kind in mongodb. I want the sorting to be based on the cities my logged in user has lived e.g If the logged in user has lived in three different cities [“California”, “Enugu”, “Manchester”].
I want the data to be sorted in this format
[
{
"_id" : "08",
"name" : "Chris",
"age" : "24",
"location" : {
"citiesLived":[
{"name":"California"},
{"name":"Ontario"},
{"name":"Texas"}
]
}
},
{
"_id" : "12",
"name" : "Jane",
"age" : "32",
"location" : {
"citiesLived":[
{"name":"Enugu"},
{"name":"Abuja"},
{"name":"Lagos"}
]
}
},
{
"_id" : "34",
"name" : "Ben",
"age" : "24",
"location" : {
"citiesLived":[
{"name":"New York"},
{"name":"Manchester"},
{"name":"Nova Scotia"}
]
}
},
{
"_id" : "1",
"name" : "John",
"age" : "28",
"location" : {
"citiesLived":[
{"name":"London"},
{"name":"Toronto"},
{"name":"Paris"}
]
}
},
]
I used mongodb $function aggregation operator in achieving this, but it seems like mongodb does not support server-side javascript on atlas.
What is the best approach to this and how do I go about it?