Given: Documents looks like this:
Supplier Document
{
"_id" : "63",
"companyName" : "Niranjan Company",
"createdBy" : "niranjank4523@gmail.com",
"createdOn" : ISODate("2017-07-06T03:12:13.171Z"),
"active" : true,
"locations" : [
"594fbfc9ae0638325796a17c",
"594fbfc9ae0638325796a17d",
"594fbfc9ae0638325796a17e"
],
"serviceArea" : "Nationwide",
},
"updatedDate" : ISODate("2017-07-24T06:29:42.221Z")
}
Locations document
{
"_id" : ObjectId("594fbfc9ae0638325796a17c"),
"location" : "New York"
}
Requirement: Sort the collection in below manner
- Supplier documents are sorted by the number of locations.
- Locations will be alphbetically sorted.
- Supplier with no locations will appear at the end.
Though I am able to sort it by number of locations in it, still unable to resolve the second point of requirement in which I have to furthur sort the data alphabetically on the basis of location and put the suppliers without any locations at the end.
Note: We can not change the structure due to some constraints from the client.