How to get the total page number for pagination

我们两清 提交于 2020-02-06 08:01:15

问题


I try to make my pageable data for weeks everything works but I would like to know the total number of pagination pages here are the data of my mongonDB.

{
  "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
}

Now here is the data paging code

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

回答1:


Mongodb 3.4 has introduced $facet aggregation which processes multiple aggregation pipelines within a single stage on the same set of input documents.

Using $facet and $group you can find documents with $limit and can get total count.

You can use below aggregation in mongodb 3.6

db.collection.aggregate([
  { "$facet": {
    "totalData": [
      { "$match": { }},
      { "$skip": 10 },
      { "$limit": 10 }
    ],
    "totalCount": [
      { "$count": "count" }
    ]
  }}
])


来源:https://stackoverflow.com/questions/59988190/how-to-get-the-total-page-number-for-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!