Skip to content
Advertisement

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

Advertisement

Answer

After Gibbs answer I had an idea (to check if query was executing the correct variables) and I found the problem, but it was in my @Document collection name.

First I added Query Logs on my console application to see if it was working well, and It was!

To check logs I Added this line on my application.properties logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

And I saw that Query was correct by log, but the collection name that the Query executing was incorrect, It was executing on messagesUser instead messagesuser, so I changed the collection name in my Entity MessagesUser and it worked.

After (It solved and It’s Working)

@Document(collection="messagesuser")
public class MessagesUser{

    public MessagesUser(String user_id, Long time) {
        this.user_id = user_id;
        this.time = time;
    }
// [continuation of the entity code...]

Before (It wasn’t working)

@Document
public class MessagesUser{

    public MessagesUser(String user_id, Long time) {
        this.user_id = user_id;
        this.time = time;
    }
// [continuation of the entity code...]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement