{"id":403,"date":"2014-02-12T09:28:30","date_gmt":"2014-02-12T09:28:30","guid":{"rendered":"http:\/\/www.andreagirardi.it\/blog\/?p=403"},"modified":"2014-06-30T19:34:18","modified_gmt":"2014-06-30T19:34:18","slug":"result-pagination-mongodb-java","status":"publish","type":"post","link":"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/","title":{"rendered":"Results pagination with MongoDB and Java"},"content":{"rendered":"<p>To implement the MongoDB results pagination in Java, there are few parameters that need to be collected:<\/p>\n<p>1) the order of results (ascending \/ descending)<br \/>\n2) the field used to order the results<br \/>\n3) the number of element and the page selected<br \/>\n4) the field and the value used to filter the results<\/p>\n<p>As well as the results of query, the method needs to return the total number of elements. All returned elements will be saved in a HashMap.<\/p>\n<pre lang=\"java\">HashMap&lt;String, Object&gt; resultMap = new HashMap&lt;String, Object&gt;();\r\n\r\nDirection direction = Sort.DEFAULT_DIRECTION;\r\nif (sortDirection &gt; 0) {\r\n\tdirection = Sort.Direction.ASC;\r\n} else { \r\n\tdirection = Sort.Direction.DESC;\r\n}\r\n\r\nList<\/pre>\n<p>If a pagination is required, <b>skip<\/b> and <b>limit<\/b> are used<\/p>\n<pre lang=\"java\">\r\nif (pageSize &gt; 0) {\r\n\tquery.skip((pageNum - 1 ) * pageSize);\r\n\tquery.limit(pageSize);\r\n}\r\n\r\nif ( sortField != null &amp;&amp; !sortField.equals(\"\") ) {\t\t\t\t\r\n\tquery.with(new Sort(direction, sortField));\r\n}\r\n\r\nresults = mongoTemplate.find(query, Object.class);<\/pre>\n<p>If a pagination is required, <b>queryCounter<\/b> (basically a version of query without pagination an limit) is used to calculate the total number of results. Of course, if pagination is not required, is possible to use directly the size of results.<\/p>\n<pre lang=\"java\">if ( pageSize &gt; 0 ) {\r\n\tresultMap.put(\"RESULT_SIZE\", (int) mongoTemplate.count(queryCounter, Object.class));\r\n} else {\r\n\t\/\/ If pagination is not required, the query is not re-executed\r\n\tresultMap.put(\"RESULT_SIZE\", results.size());\r\n}<\/pre>\n<p>mongoTemplate is a spring bean defined in this way on context configuration:<\/p>\n<pre lang=\"xml\">\r\n<bean id=\"mongoTemplate\" class=\"org.springframework.data.mongodb.core.MongoTemplate\"  c:mongo-ref=\"mongo\" c:databaseName=\"${mongo.db.name}\">\r\n\t<property name=\"writeConcern\" value=\"SAFE\" \/>\r\n<\/bean>\r\n<\/pre>\n<p>[AdSense-A]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-403\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-403\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-403\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-403\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>To implement the MongoDB results pagination in Java, there are few parameters that need to be collected: 1) the order of results (ascending \/ descending) 2) the field used to order the results 3) the number of element and the page selected 4) the field and the value used to filter the results As well [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-403\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-403\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-403\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-403\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/result-pagination-mongodb-java\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[14],"tags":[4,32],"_links":{"self":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/403"}],"collection":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/comments?post=403"}],"version-history":[{"count":10,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":529,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/403\/revisions\/529"}],"wp:attachment":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/media?parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/categories?post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/tags?post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}