0

I have a Post document in Mongo DB, which has an array of comments.

class Post {
 String id,
 User user,
 List<Comments> comments
}

The class structure looks as above.

Now I would like to use mongoTemplate in order to retrieve a sliced content of the comments.

Lets say there are thousands of comments, I would like to get sliced results for this NESTED ARRAY. Just to clarify, I am not looking for Paginated(OR Sliced) result of the Posts (which the ROOT document).

The query is supported by mongo, but I could not form a query from java using spring boot. https://docs.mongodb.com/manual/reference/operator/projection/slice/

How to simulate the above query using Spring Boot 2.0.0 and MongoTemplate?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Kency Kurian
  • 182
  • 2
  • 11
  • In the other question what I was looking for is Paginated or Sliced results of the document itself not of a nested element. In this case it is for the nested array. Hope I have clarified myself. – Kency Kurian Jun 05 '18 at 09:11
  • That would indeed be `$slice`, but it also already has existing answers. Switched duplicate to that one. – Neil Lunn Jun 05 '18 at 09:16
  • Thanks @NeilLunn , agreed that it is indeed a duplicate. Looks like spring does not support it as of today. Following up https://jira.spring.io/browse/DATAMONGO-1230?jql=project%20%3D%20DATAMONGO%20AND%20text%20~%20%22%24slice%22 – Kency Kurian Jun 05 '18 at 09:41
  • I noticed. Oliver does frequently look at the questions with `spring-mongodb` in the tags, so retagging your question appropriately to at least give a "nudge" that the issue needs to be dealt with. Workarounds are listed in the existing answers. – Neil Lunn Jun 05 '18 at 09:45

0 Answers0