首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取分页的总页数

问如何获取分页的总页数
EN

Stack Overflow用户
提问于 2020-01-30 22:25:12
回答 1查看 229关注 0票数 0

我试着让我的数据分页几个星期,一切正常,但我想知道分页的总数,这里是我的mongonDB的数据。

代码语言:javascript
复制
{
  "content": [
    {
      "mame": "iPhone X",
      "eategory": "High-Tech",
      "productId": "aDPXF7Xq",
      "details": {
        "vating": "00",
        "stocks": "20",
        "price": "800000",
        "tags": [
          {
            "tagsl": "Apple",
            "tags2": "aull",
            "tags3": null
          }
        ],
        "brand": "Apple",
        "description": "Iphone xX",
        "picture": [
          {
            "picturel": "photol",
            "picture2": null,
            "picture3": null,
            "picture4": null,
            "picture5": null
          }
        ],
        "thumbnails": [
          {
            "thumbnaill": "thumbails 1",
            "thumbnail2": null,
            "thumbnail3": null,
            "thumbnail4": null,
            "thumbnail5": null
          }
        ]
      }
    },
    {
      "mame": "iPhone X",
      "eategory": "High-Tech",
      "productId": "cjjVqOBk",
      "details": {
        "rating": "10",
        "stocks": "20",
        "price": "800000",
        "tags": [
          {
            "tagsl": "Apple",
            "tags2": "aull",
            "tags3": null
          }
        ],
        "brand": "Apple",
        "description": "Iphone xX",
        "picture": [
          {
            "picturel": "photol",
            "picture2": null,
            "picture3": null,
            "picture4": null,
            "picture5": null
          }
        ],
        "thumbnails": [
          {
            "thumbnaill": "thumbails 1",
            "thumbnail2": null,
            "thumbnail3": null,
            "thumbnail4": null,
            "thumbnail5": null
          }
        ]
      }
    },
    {
      "mame": "iPhone X",
      "eategory": "High-Tech",
      "productId": "LhKiRGr6",
      "details": {
        "vating": "10",
        "stocks": "20",
        "price": "800000",
        "tags": [
          {
            "tagsl": "Apple",
            "tags2": "aull",
            "tags3": null
          }
        ],
        "brand": "Apple",
        "description": "Iphone xX",
        "picture": [
          {
            "picturel": "photol",
            "picture2": null,
            "picture3": null,
            "picture4": null,
            "picture5": null
          }
        ],
        "thumbnails": [
          {
            "thumbnaill": "thumbails 1",
            "thumbnail2": null,
            "thumbnail3": null,
            "thumbnail4": null,
            "thumbnail5": null
          }
        ]
      }
    },
    {
      "mame": "iPhone X",
      "eategory": "High-Tech",
      "productId": "dgCvi8NJ",
      "details": {
        "vating": "10",
        "stocks": "20",
        "price": "800000",
        "tags": [
          {
            "tagsl": "Apple",
            "tags2": "aull",
            "tags3": null
          }
        ],
        "brand": "Apple",
        "description": "Iphone xX",
        "picture": [
          {
            "picturel": "photol",
            "picture2": null,
            "picture3": null,
            "picture4": null,
            "picture5": null
          }
        ],
        "thumbnails": [
          {
            "thumbnaill": "thumbails 1",
            "thumbnail2": null,
            "thumbnail3": null,
            "thumbnail4": null,
            "thumbnail5": null
          }
        ]
      }
    }
  ],
  "pageable": {
    "sort": {
      "sorted": false,
      "unsorted": true,
      "empty": true
    },
    "offset": 0,
    "pageNumber": 0,
    "pageSize": 20,
    "paged": true,
    "unpaged": false
  },
  "totalPages": 1,
  "totalElements": 4,
  "last": true,
  "size": 20,
  "sort": {
    "sorted": false,
    "unsorted": true,
    "empty": true
  },
  "number": 0,
  "numberOfElements": 4,
  "first": true,
  "empty": false
}

下面是数据分页代码

代码语言:javascript
复制
@GetMapping("/engine/search")
public PageImpl<Products> engineSearch(@RequestParam("p") String query, @RequestParam(value = "page", defaultValue = "0") int page, Pageable pageable) {
    Criteria criteria = new Criteria();
    final Aggregation aggregation = Aggregation.newAggregation(

            Aggregation.match( criteria.orOperator(
                    Criteria.where( "name" ).regex( query, "i" ),
                    Criteria.where( "details.brand" ).regex( query, "i" ),
                    Criteria.where( "details.tags.tags1" ).regex( query, "i" ),
                    Criteria.where( "details.tags.tags2" ).regex( query, "i" ),
                    Criteria.where( "details.tags.tags3" ).regex( query, "i" )
                    )),
            Aggregation.skip(page * 4),
            Aggregation.limit(4)

    );
    List<Products> filter = mongoTemplate.aggregate(aggregation, "Product", Products.class).getMappedResults();
    return new PageImpl<Products>(filter, pageable, filter.size());
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-30 22:29:39

MongoDB3.4引入了$facet聚合,它在同一组输入文档上的单个阶段处理多个聚合管道。

使用$facet和$group,您可以使用$limit查找文档,并可以获得总计数。

代码语言:javascript
复制
You can use below aggregation in mongodb 3.6

db.collection.aggregate([
  { "$facet": {
    "totalData": [
      { "$match": { }},
      { "$skip": 10 },
      { "$limit": 10 }
    ],
    "totalCount": [
      { "$count": "count" }
    ]
  }}
])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59988190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档