0

in my Collection I have this kind of documents:

{
"_id" : ObjectId("5ebf6e2293c4b6fca459c03a"),
"country" : "DE",
"name" : "Karlshuld",
"lat" : "48.6819",
"lng" : "11.28503"
}

As you can see, it's a "database" with names of cities and their geo coordinates. My use case for this data is, that a user enters a (partial) name of the city and gets a list of matching documents.

My problem is, that I have to sort the data, because if a user just type in 3 letters, there are globally a lot of cities starting with the given string.

So I want to do a kind of "custom sorting". If a user is a german user, I want to sort the results by country, where "DE" comes first, following by "AT" and "CH". And then the other results.

How can I do this?

user39063
  • 183
  • 1
  • 13
  • Does this answer your question? [Mongo complex sorting?](https://stackoverflow.com/questions/3212919/mongo-complex-sorting) – Valijon May 16 '20 at 08:57
  • For your case, I think it's better to separate your queries. One with that queries only the users country, the other one without. Then join the results together – thammada.ts May 16 '20 at 10:08

0 Answers0