2015-07-03 124 views
2

问题聚集查询MongoDB的Java驱动程序使用极限

查询工作正常,但不限制,并跳过,这是一次获取所有记录。

请提出我做错了什么。

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(); 

感谢

回答

3

聚合管道在运行 “连续”。这些都不是喜欢.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核心文档。

+0

我试过了。我得到错误 - 方法skip(int)未定义 –

+0

@ Rajnishkatiyar-SIPL因此'skip'的变量未定义!当然,这应该是一个明显的错误。我所做的一点是你的基本呼叫顺序是不正确的。 –

+0

我可以向你保证,这不是一个问题,因为我也尝试过这样 - skip(0),limit(5) –

相关问题