分组时将分页结果的MongoDB聚合添加到Java代码中的其他字段

分组时将分页结果的MongoDB聚合添加到Java代码中的其他字段,java,spring,mongodb,spring-data-mongodb,Java,Spring,Mongodb,Spring Data Mongodb,使用answer,我创建了一个Mongodb查询,它对对象进行分页和排序,并添加对象的总数。我现在不能做的是把它翻译成java代码 db.messages.aggregate([ { $match: { _id: {$exists: true} }, { $sort: { _id: 1 } }, // here you can sort using other field { $group: { _id: null, messagesCount:

使用answer,我创建了一个Mongodb查询,它对对象进行分页和排序,并添加对象的总数。我现在不能做的是把它翻译成java代码

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
    }
  } }
])
配对操作和分类操作非常简单。 java代码:

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);
MessagesSortedAndPaginated类:

public class MessagesSortedAndPaginated {
    private long totalCount;
    private List<Message> messagesPage;
}

我真的花了好几个小时来解决这个问题。以下是缺失的操作:

GroupOperation groupOperation = Aggregation.group().count().as("messagesCount").push(Aggregation.ROOT).as("messagesPage");
ProjectionOperation projectOperation = Aggregation.project().andExpression("messagesCount").as("messagesPage")

您可以通过在MongoDB聚合管道中使用$skip$limit来达到此目的,例如:

{
    "aggregate":  "messages",
    "pipeline": [
         {
             "$match": {
                 "$or":
                     [{"resourceType": "email"}, {"resourceType": "address"},{"resourceType": "telephone"} ]
             }
         },
         {
             "$project": {
                "ID":       "$resources.id",
                "CLIENTID": "$resources.clientId"
                .
                .
             }
         },{
             "$skip": ${fromId}
         }
         ,{
             "$limit": ${fetchSize}
         }
    ]
}
还可以使用MongoTemplate执行:

DBObject dbObject = (BasicDBObject) JSON.parse(scriptNoSql);
                if (null == dbObject) {
                    return;
                }
                DB db = mongoTemplate.getDb();
                CommandResult result = db.command(dbObject);


                if(!result.ok()) {
                    throw result.getException();
                }
DBObject dbObject = (BasicDBObject) JSON.parse(scriptNoSql);
                if (null == dbObject) {
                    return;
                }
                DB db = mongoTemplate.getDb();
                CommandResult result = db.command(dbObject);


                if(!result.ok()) {
                    throw result.getException();
                }