Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Java驱动程序在聚合查询中的使用限制_Java_Mongodb_Aggregation Framework_Spring Data Mongodb - Fatal编程技术网

Mongodb Java驱动程序在聚合查询中的使用限制

Mongodb Java驱动程序在聚合查询中的使用限制,java,mongodb,aggregation-framework,spring-data-mongodb,Java,Mongodb,Aggregation Framework,Spring Data Mongodb,问题 查询工作正常,但没有限制和跳过,它一次获取所有记录 请指出我做错了什么 MongoDB系列 { "_id" : ObjectId("559666c4e4b07a176940c94f"), "postId" : "559542b1e4b0108c9b6f390e", "user" : { "userId" : "5596598ce4b07a176940c943",

问题

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

请指出我做错了什么

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();
Aggregation=newAggregation(
匹配(标准,其中(“isActive”)为(1)和(feedUsers)中的(“user.userId”),
集团(“职位”)
.最后一个(“职位”)。作为(“职位”)
.最后一次(“postFor”)。作为(“postFor”)
.last(“actionType”)。作为(“actionType”)
.last(“isActive”)。作为(“isActive”)
.最后一位(“用户”)。作为(“用户”)
.last(“createdDate”).as(“createdDate”)
.最后一次(“更新日期”)。作为(“更新日期”),
排序(sort.Direction.DESC,“createdDate”)
);
聚合.跳过(skip);
加总.限额(限额);
聚合结果groupResults=
聚合(聚合,SocialActionsTrail.class,UserFeedAggregation.class);
返回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(int)为undefined@Rajnishkatiyar-SIPL,因此
skip
的变量未定义!当然,这应该是一个明显的错误。我想说的是,您的基本呼叫顺序不正确。我可以向您保证,这不是问题,因为我也尝试过这样做-跳过(0),限制(5)@rajinskatiyar SIPL您需要几秒钟以上的时间来响应。运行代码
.skip(0)
不会导致“未定义”。@BlakesSeven您在这方面是对的,但是mongodb java spring驱动程序没有像sort和group这样的skip和limit方法。我也有同样的问题,跳过和限制聚合函数。