Как получить общий номер страницы для нумерации страниц - PullRequest
0 голосов
/ 30 января 2020

Я пытаюсь сделать свои страницы доступными для данных в течение нескольких недель, все работает, но я хотел бы знать, что общее количество страниц разбивки на страницы - это данные моего 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
}

Теперь вот код подкачки данных.

@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 Ответ

0 голосов
/ 30 января 2020

Mongodb 3.4 представил агрегацию $facet, которая обрабатывает несколько конвейеров агрегации в пределах одной стадии на одном наборе входных документов.

Используя $facet и $group, вы можете найти документы с помощью $limit и может получить общее количество.

You can use below aggregation in mongodb 3.6

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