I have a usecase like this. I have questions in my Mongo DB and have a CRUD micro service. There I have exposed an API method to fetch questions by list of IDs given via query params. Let say for the simplicity sake, user gives /api/questions?id=2, id=7, id=4, id = 5
then I need to return list of questions in that exact same order, like so
questions: [
{
id: 2,
prompt: "prompt one",
...
},
{
id: 7,
prompt: "prompt two",
...
},
{
id: 4,
...
},
{
id: 5
...
}
]
But notice that this is neither ASC nor DESC, rather can be any arbitrary order like /api/questions?id=2, id=7, id=4, id=5
I am using org.springframework.data.mongodb.repository.MongoRepository as my DAO class. Currently I am doing ordering inside my service layer after fetching the data through the repository which is based on spring-data. That solution works. But I would rather prefer to get this ordering done at the DAO repository level itself, since it costs less and does not add unnecessary complexity to my business logic at the service layer. Rather I can delegate the responsibility merely to the DB, assuming it is more capable of doing such things with more optimizations.
The Mongo document structure looks like this.

Can I achieve this using spring data? If so how to get that thing done?