What’s the best way to sort a MongoDB query result in Spring MVC? I’ve got all the mappings set up in the XML files and I can call the myrepository.findAll() in the Service class just fine and it’s working great.
How can I sort those results (by date for example) and/or limit the results (like by 3, etc). In Mongo itself it’s pretty easy to just do a db.mycollection.find().sort({ postedAt : -1}); Or just throw a .limit(3) to get the first 3 results.
What’s the best way to handle this in a Spring MVC environment?
Thanks.
If your myrepository object is an instance of MongoRepository, you could call the findAll method with a Pageable object as parameter.
Actually, this method is from PagingAndSortingRepository interface.