'Aggregation Annotation Not Working It is Returning Empty
I am trying to make this Aggregation bellow using Spring Java, but it's returning an Empty List on my console. This is working on MongoDB, but not on my spring application.
This is my MongoDB Query:
db.collection.aggregate([
{
$match: {
$and: [
{
user_id: "256f5280-fb49-4ad6-b7f5-65c4329d46e0"
},
{
time: {
$gte: 1622471890,
$lt: 1822471890
}
}
]
}
},
{
$sort: {
time: -1
}
}
])
It's return me 2 results:
[
{
"_id": ObjectId("5a934e000102030405000001"),
"message": "This is an example of my db (1)",
"time": 1.62247189e+09,
"user_id": "256f5280-fb49-4ad6-b7f5-65c4329d46e0"
},
{
"_id": ObjectId("5a934e000102030405000002"),
"message": "This is an example of my db (2)",
"time": 1.62247189e+09,
"user_id": "256f5280-fb49-4ad6-b7f5-65c4329d46e0"
}
]
My Problem is when I do this using Mongo with Spring Java:
My Repository
@Aggregation(pipeline = {
"{" +
"$match: {" +
"$and: [" +
"{" +
"user_id: ?2" +
"}," +
"{" +
"time: {" +
"$gte: ?0," +
"$lt: ?1" +
"}" +
"}" +
"]" +
"}" +
"}",
"{" +
"$sort: {" +
"time: -1" +
"}" +
"}"
})
List<MessagesUser> findByPeriodAndUserIdPaginated(long from, long to, String user_id, Pageable pageable);
A part of My Service
@Override
public Page<MessagesUser> findAllBetween(Date from, Date to, Pageable page, String user_id) {
List<MessagesUser> messagesUserList = messagesUserRepository.findByPeriodAndUserIdPaginated(from.getTime() / 1000, to.getTime() / 1000, user_id, page);
System.out.println("messagesUserList: ");
System.out.println(messagesUserList); // It is the empty list
This Java Query Repository is returning to me an empty array instead of my 2 values. You should see the Dataset and an example working well on MongoDB Query here: Mongo playground
Solution 1:[1]
There is a problem with your annotation
You have used
"user_id: ?2" +
but user_id is a 4th param then you need to use
"user_id: ?3" +
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gibbs |
