Guest User

Untitled

a guest
Jun 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. db.messages.aggregate([
  2. { $match: { _id: {$exists: true} },
  3. { $sort: { _id: 1 } }, // here you can sort using other field
  4. { $group: {
  5. _id: null,
  6. messagesCount: { $sum: 1 },
  7. allMessages: {
  8. $push: '$$ROOT'
  9. }
  10. } }
  11. { $project: {
  12. _id: 0,
  13. messagesCount: 1,
  14. messagesPage: {
  15. $slice: ['$allMessages', 0, 30] //pageNo=0, pageSize=30
  16. }
  17. } }
  18. ])
  19.  
  20. MatchOperation matchOperation = new MatchOperation(Criteria.where("_id").exists(true));
  21. SortOperation sortOperation = new SortOperation(new Sort(Sort.Direction.DESC, "_id"));
  22. //HOW DO I TRANSLATE THESE TWO IN JAVA CODE?
  23. GroupOperation groupOperation = Aggregation.group()....**???**
  24. ProjectionOperation projectOperation = Aggregation.project()...**???**
  25.  
  26.  
  27. mongoTemplate.aggregate(
  28. newAggregation(matchOperation, sortOperation, groupOperation, projectOperation), "messages", MessagesSortedAndPaginated.class);
  29.  
  30. public class MessagesSortedAndPaginated {
  31. private long totalCount;
  32. private List<Message> messagesPage;
  33. }
  34.  
  35. @Document(collection = "messages")
  36. public @Data class Message implements Serializable {
  37.  
  38. private static final long serialVersionUID = 1L;
  39.  
  40. @Id
  41. private String id;
  42. ...
  43.  
  44. GroupOperation groupOperation = Aggregation.group().count().as("messagesCount").push(Aggregation.ROOT).as("messagesPage");
  45. ProjectionOperation projectOperation = Aggregation.project().andExpression("messagesCount").as("messagesPage")
Add Comment
Please, Sign In to add comment