How to query the same field twice using the Mongo Repository?

Waray Mohamed Amine :

So i want to make a query that looks for all tasks existing in my collection, with the criteria of having a date attribute between two given dates. I have used the Between keyword, but it's not including the values of the parameters in results.
For example if the parameters are 2019-09-20 and 2019-09-25, the tasks that are planned for those same dates won't be returned in the response.

I have tried to use LessThanEqual and GreaterThanEqual, but i get the following error :

org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDocument, you can't add a second 'task.date' expression specified as 'task.date : Document{{$lte=Thu Sep 26 00:00:00 CEST 2019}}'. Criteria already contains 'task.date : Document{{$gte=Wed Sep 25 00:00:00 CEST 2019}}'.

And that's why i want to know if there's a way to query twice on the same field.

This is my repository.

public interface TaskRepository extends MongoRepository<TaskEntry, String> {
    List<TaskEntry> findByTask_DateGreaterThanEqualAndTask_DateLessThanEqual(Date dateFrom, Date dateTo);
}

Thanks.

UPDATE :
This is an illustrating example of my collection, query and the result after using rustyx's answer.
These are my documents :

[
    {
        "id": "5c3da5b163c2789768d1402a",
        "code": "0000001",
        "createdDate": "2019-01-15T09:19:45.114+0000",
        "lastModifiedDate": "2019-01-15T09:19:45.114+0000",
        "task": {
            "codeRule": null,
            "title": "Task 1",
            "date": "2019-09-26T00:00:00.000+0000",
            "description": "This is the first task",
            "type": "UNITARY",
            "result": null
        }
    },
    {
        "id": "5c3da7dc63c2789768d1402b",
        "code": "0000002",
        "createdDate": "2019-01-15T09:29:00.125+0000",
        "lastModifiedDate": "2019-01-15T09:29:00.125+0000",
        "task": {
            "codeRule": null,
            "title": "Task 2",
            "date": "2019-09-26T00:00:00.000+0000",
            "description": "This is the second task",
            "type": "UNITARY",
            "result": null
        }
    },
    {
        "id": "5c3db0c763c27868b41e022b",
        "code": "0000003",
        "createdDate": "2019-01-15T10:07:03.693+0000",
        "lastModifiedDate": "2019-01-15T10:07:03.693+0000",
        "task": {
            "codeRule": null,
            "title": "Task 3",
            "date": "2019-09-28T00:00:00.000+0000",
            "description": "This is the third task",
            "type": "UNITARY",
            "result": null
        }
    }
]

And this is the response i get by using this:

@Query("{'task.date': {$gte: ?0, $lte:?1 }}")
    List<TaskEntry> findByTask_DateBetween(Date dateFrom, Date dateTo);

And this :

/tasks?dateFrom=2019-09-26&dateTo=2019-09-28

Response :

[
    {
        "id": "5c3da5b163c2789768d1402a",
        "code": "0000001",
        "createdDate": "2019-01-15T09:19:45.114+0000",
        "lastModifiedDate": "2019-01-15T09:19:45.114+0000",
        "task": {
            "codeRule": null,
            "title": "Task 1",
            "date": "2019-09-26T00:00:00.000+0000",
            "description": "This is the first task",
            "type": "UNITARY",
            "result": null
        }
    },
    {
        "id": "5c3da7dc63c2789768d1402b",
        "code": "0000002",
        "createdDate": "2019-01-15T09:29:00.125+0000",
        "lastModifiedDate": "2019-01-15T09:29:00.125+0000",
        "task": {
            "codeRule": null,
            "title": "Task 2",
            "date": "2019-09-26T00:00:00.000+0000",
            "description": "This is the second task",
            "type": "UNITARY",
            "result": null
        }
    }
]

The tasks with date 2019-09-26 are being added to respone which means $gte: ?0 is working, but the task with date 2019-09-28 in not there.

rustyx :

You can use "Between":

    List<TaskEntry> findByTask_DateBetween(Date dateFrom, Date dateTo);

If that doesn't suffice, use a custom query:

    @Query("{'date': {$gte: ?0, $lte:?1 }}")
    List<TaskEntry> findByTask_DateBetween(Date dateFrom, Date dateTo);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161408&siteId=1