Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- db.messages.aggregate([
- { $match: { _id: {$exists: true} },
- { $sort: { _id: 1 } }, // here you can sort using other field
- { $group: {
- _id: null,
- messagesCount: { $sum: 1 },
- allMessages: {
- $push: '$$ROOT'
- }
- } }
- { $project: {
- _id: 0,
- messagesCount: 1,
- messagesPage: {
- $slice: ['$allMessages', 0, 30] //pageNo=0, pageSize=30
- }
- } }
- ])
- MatchOperation matchOperation = new MatchOperation(Criteria.where("_id").exists(true));
- SortOperation sortOperation = new SortOperation(new Sort(Sort.Direction.DESC, "_id"));
- //HOW DO I TRANSLATE THESE TWO IN JAVA CODE?
- GroupOperation groupOperation = Aggregation.group()....**???**
- ProjectionOperation projectOperation = Aggregation.project()...**???**
- mongoTemplate.aggregate(
- newAggregation(matchOperation, sortOperation, groupOperation, projectOperation), "messages", MessagesSortedAndPaginated.class);
- public class MessagesSortedAndPaginated {
- private long totalCount;
- private List<Message> messagesPage;
- }
- @Document(collection = "messages")
- public @Data class Message implements Serializable {
- private static final long serialVersionUID = 1L;
- @Id
- private String id;
- ...
- GroupOperation groupOperation = Aggregation.group().count().as("messagesCount").push(Aggregation.ROOT).as("messagesPage");
- ProjectionOperation projectOperation = Aggregation.project().andExpression("messagesCount").as("messagesPage")
Add Comment
Please, Sign In to add comment