java - Mongodb Java 驱动在聚合查询中的使用限制

标签 java mongodb aggregation-framework spring-data-mongodb

问题

查询工作正常但没有限制和跳过,它正在一次获取所有记录。

请指出我做错了什么。

MongoDB 集合

 {  
      "_id" : ObjectId("559666c4e4b07a176940c94f"),     
      "postId" : "559542b1e4b0108c9b6f390e",    
     "user" : {         
                 "userId" : "5596598ce4b07a176940c943",         
                 "displayName" : "User1",       
                 "username" : "user1",      
                 "image" : ""   
      },    
      "postFor" : {         
                     "type": "none",
                      "typeId" : ""     
      },    
     "actionType" : "like",     
     "isActive" : 1,
     "createdDate" : ISODate("2015-07-03T10:41:07.575Z"),   
     "updatedDate" : ISODate("2015-07-03T10:41:07.575Z") 
}

Java驱动查询

 Aggregation aggregation = newAggregation(                                

      match(Criteria.where("isActive").is(1).and("user.userId").in(feedUsers)),
      group("postId")
      .last("postId").as("postId")
      .last("postFor").as("postFor")
      .last("actionType").as("actionType")
      .last("isActive").as("isActive")
      .last("user").as("user")
      .last("createdDate").as("createdDate")
      .last("updatedDate").as("updatedDate"),
      sort(Sort.Direction.DESC, "createdDate")
    );
aggregation.skip( skip );
aggregation.limit( limit );
AggregationResults<UserFeedAggregation> groupResults =
mongoOps.aggregate(aggregation, SocialActionsTrail.class, UserFeedAggregation.class);
return groupResults.getMappedResults();

谢谢

最佳答案

聚合管道在操作中是“顺序的”。这些不像 .find() 操作,其中 .sort() .limit() .skip()是查询操作的“修饰符”:

Aggregation aggregation = newAggregation(                               
    match(Criteria.where("isActive")
        .is(1).and("user.userId").in(feedUsers)),
    group("postId")
        .last("postId").as("postId")
        .last("postFor").as("postFor")
        .last("actionType").as("actionType")
        .last("isActive").as("isActive")
        .last("user").as("user")
        .last("createdDate").as("createdDate")
        .last("updatedDate").as("updatedDate"),
    sort(Sort.Direction.DESC, "createdDate"),
    skip( skip ),
    limit( limit )
);

除非您在“顺序”中定义操作,否则管道不知道执行顺序。因此,将管道定义为一个整体。


一个基本的例子:

Aggregation aggregation = newAggregation(
    group("postId"),
    skip(1),
    limit(1)
);

System.out.println(aggregation)

输出一个完美的流水线:

{ 
    "aggregate" : "__collection__" ,
    "pipeline" : [ 
        { "$group" : { "_id" : "$postId" } },
        { "$skip" : 1 },
        { "$limit" : 1 }
    ]
}

参见 $skip$limit在核心文档中。

关于java - Mongodb Java 驱动在聚合查询中的使用限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31206797/

相关文章:

java - MongoDB 聚合框架和 Java 驱动程序使 $or 条件起作用

java - 在 JBOSS 6 AS 上配置 TLS 会导致 ERR_SSL_VERSION_OR_CIPHER_MISMATCH(在 Chrome 中)或 ssl_error_no_cypher_overlap(在 Mozilla 中)错误

java - 如何使用servlet/jsp显示数据库中的数据

linux - 在 shell 中运行 mongodb 可以,但它不能作为服务运行

.net - Mongodb .net 异步等待

python - MongoDB 和 PyMongo : Upsert multiple values

java - 如何在 Spring + Thymeleaf 上添加弹出窗口

java - 在 java 中创建一个本地押韵字典

javascript - 根据某些条件,使用 mongodb 聚合从数组中获取对象以及同一级别的其他字段

mongodb - 在没有 $ 运算符的情况下使用 mongodb 中的子数组元素更新多个文档