小编Ste*_*rck的帖子

Spring Data mongodb聚合管道的分页结果

我在对聚合管道的结果进行分页时遇到了一些麻烦。在查看了In spring data mongodb 如何实现分页进行聚合后,我想出了一个感觉像 hacky 的解决方案。我首先执行匹配查询,然后按我搜索的字段分组,并对结果进行计数,将值映射到一个私有类:

private long getCount(String propertyName, String propertyValue) {
    MatchOperation matchOperation = match(
        Criteria.where(propertyName).is(propertyValue)
    );
    GroupOperation groupOperation = group(propertyName).count().as("count");
    Aggregation aggregation = newAggregation(matchOperation, groupOperation);
    return mongoTemplate.aggregate(aggregation, Athlete.class, NumberOfResults.class)
        .getMappedResults().get(0).getCount();
}

private class NumberOfResults {
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,我就能够为我返回的页面对象提供一个“总”值:

public Page<Athlete> findAllByName(String name, Pageable pageable) {
    long total = getCount("team.name", name);
    Aggregation aggregation = getAggregation("team.name", name, pageable); …
Run Code Online (Sandbox Code Playgroud)

spring-data-mongodb spring-boot spring-restcontroller spring-rest

5
推荐指数
1
解决办法
5560
查看次数

Google云发布/订阅订阅的持久性?

我已经在他们的网站上阅读了有关Google Cloud Pub / Sub的信息,它看起来像是已实现的消息传递解决方案,但我会考虑将其移至该解决方案,以便我的团队不必维护该代码库。到目前为止,我还没有找到答案的一个细节是有关服务重新启动后订阅,主题等的持久性。我可能已经错过了,我相信必须在某个地方回答这个问题,但是如果有人可以指出我的信息,我将不胜感激。提前致谢!

google-cloud-messaging google-cloud-platform google-cloud-pubsub

3
推荐指数
1
解决办法
355
查看次数