Cody Ferguson
Cody Ferguson

Reputation: 393

Auto-Generated Spring data query for MongoDB: X most recent entries

I have been using this tutorial/guide to create auto-generated queries for a Mongo database. All but one of the methods I created have worked and I am wondering if anyone could give me some feedback on what I am doing wrong with this method:

List<DcrExecution> findDcrExecutionsByLimitOrderByExecutionTimeDesc(int num);

For now, I would like to avoid creating my own custom method. However, if that is the only option, then I will look into it. I want to find the X most recent dcrExecutions with X being a value the user chooses. So, I am new to spring, and I am unsure of the order in which to put the commands in the method so that Spring can generate a query method from it. I have tried some other variations of the method signature, for example:

List<DcrExecution> findDcrExecutionsOrderByExecutionTimeDescLimitBy(int num);

but this created the error:

No property descLimitBy found for type Date!

which is no where near what I was trying to achieve. I would like to create a method which will find the X most recent entries in the Mongo database. Order by will order them by date then desc will give me the most recent entries first. The limit or limit by will only retrive the top amount of entries specified by the parameter num. That is my thought process, anyways. Any help would be great!

Here is the full interface class:

public interface DcrExecutionRepo extends MongoRepository<DcrExecution, String> {
    List<DcrExecution> findDcrExecutionsByExecutionTimeAfter(Date date);

    //TODO: Fix
    //List<DcrExecution> findDcrExecutionsByLimitOrderByExecutionTimeDesc(int num);

    List<DcrExecution> findDcrExecutionsByFinishedIsFalse();

Upvotes: 1

Views: 1064

Answers (1)

Jaiwo99
Jaiwo99

Reputation: 10017

Check out the reference here: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repository-query-keywords limit is not a supported keyword

Upvotes: 1

Related Questions