Custom sorting on nested fields with array of objects

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?

Hi @Ekene_Samson,

What’s the use case here? Once you retrieve the documents you require, have you tried sorting them on the application end?

Fwiw, I also tested using $sortArray on the sample documents you provided. Perhaps it may work for you?:

db.coll.aggregate({
   "$project":{
      "id":1,
      "name":1,
      "age":1,
      "location.citiesLived":{
         "$sortArray":{
            "input":"$location.citiesLived",
            "sortBy":{
               "name":1
            }
         }
      }
   }
})
[
  {
    _id: '1',
    name: 'John',
    age: '28',
    location: {
      citiesLived: [ { name: 'London' }, { name: 'Paris' }, { name: 'Toronto' } ]
    }
  },
  {
    _id: '12',
    name: 'Jane',
    age: '32',
    location: {
      citiesLived: [ { name: 'Abuja' }, { name: 'Enugu' }, { name: 'Lagos' } ]
    }
  },
  {
    _id: '34',
    name: 'Ben',
    age: '24',
    location: {
      citiesLived: [
        { name: 'Manchester' },
        { name: 'New York' },
        { name: 'Nova Scotia' }
      ]
    }
  },
  {
    _id: '08',
    name: 'Chris',
    age: '24',
    location: {
      citiesLived: [
        { name: 'California' },
        { name: 'Ontario' },
        { name: 'Texas' }
      ]
    }
  }
]

Please see the below documentation regarding this:

You can configure this for M10+ tier clusters in the additional settings section when editing a cluster configuration. As per the M0, M2/M5 tier limitations:

M0 free clusters and M2/M5 shared clusters don’t support server-side JavaScript. For example, $where and map-reduce are unsupported.

Regards,
Jason

I want the returned that to look something like this;

[
  {	
	"_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"}
        ]
      }
},
]

Based on this criteria;
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”].

Did you read the output from my above aggregation using $sortArray?

Do you just want the location.citiesLived array sorted by the name field from the documents within the location.citiesLived array?