{"id":1533,"date":"2012-07-14T15:00:00","date_gmt":"2012-07-14T15:00:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/sorting-spring-data-mongodb-collections-using-orderby.html"},"modified":"2012-10-22T06:01:37","modified_gmt":"2012-10-22T06:01:37","slug":"sorting-spring-data-mongodb-collections","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html","title":{"rendered":"Sorting Spring Data MongoDB collections using @OrderBy"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">This is already third post about tuning and enhancing <a href=\"http:\/\/www.springsource.org\/spring-data\/mongodb\">Spring Data MongoDB<\/a> capabilities. This time I found that I miss one JPA feature \u2013 <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/persistence\/OrderBy.html\">@OrderBy<\/a> annotation. @OrderBy specifies the ordering of the elements of a collection valued association at the point when the association is retrieved.         <\/p>\n<p>In this article I will show how to implement <strong>sorting<\/strong> with <strong>@OrderBy<\/strong> annotation with <strong>Spring Data MongoDB<\/strong>.         <strong>&nbsp;<\/strong><\/p>\n<p><strong>Use case<\/strong>        <\/p>\n<p>Just a short example of what is it all about for those who did not use JPA @OrderBy before. We\u2019ve got here two classes and one to many relation:         <\/p>\n<pre class=\"brush:java\">package pl.maciejwalkowiak.springdata.mongodb.domain;\r\n\r\nimport org.bson.types.ObjectId;\r\nimport org.springframework.data.annotation.Id;\r\nimport org.springframework.data.mongodb.core.mapping.Document;\r\n\r\n@Document\r\npublic class Backpack {\r\n @Id\r\n private ObjectId id;\r\n\r\n private List&lt;Item&gt; items;\r\n\r\n ...\r\n}\r\n<\/pre>\n<pre class=\"brush:java\">public class Item {\r\n private String name;\r\n private int price;\r\n\r\n ...\r\n}\r\n<\/pre>\n<p>Backpack is here a main class and contains list of embedded items. When Backpack is loaded from database its items are loaded in order close to insertion order. What if we want to change that and order items by one of its fields? We need to implement sorting by our own and again we will extend <a href=\"http:\/\/static.springsource.org\/spring-data\/data-mongo\/docs\/1.0.1.RELEASE\/api\/org\/springframework\/data\/mongodb\/core\/mapping\/event\/AbstractMongoEventListener.html\">AbstractMongoEventListener<\/a>.         <\/p>\n<p><strong>Sorting details: introducing @OrderBy<\/strong>        <\/p>\n<p>In opposite to JPA \u2013 sorting in this case sorting cannot be done on database level. We need to take care about it on application side \u2013 that can be done in two places:         <\/p>\n<ul>\n<li>before object is converted into MongoDB data structure \u2013 if we want to make sure that objects are sorted properly inside MongoDB collection<\/li>\n<li>after object is converted from MongoDB data structure into Java object \u2013 if we just want to make sure that inside our application List is sorted properly<\/li>\n<\/ul>\n<p>In order to specify in which place sorting should take place I have created SortPhase enumeration:         <div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">public enum SortPhase {\r\n AFTER_CONVERT,\r\n BEFORE_CONVERT;\r\n}\r\n<\/pre>\n<p>Finally \u2013 @OrderBy annotation will contain three almost self describing properties:         <\/p>\n<pre class=\"brush:java\">package pl.maciejwalkowiak.springdata.mongodb;\r\n\r\nimport org.springframework.data.mongodb.core.query.Order;\r\n\r\nimport java.lang.annotation.ElementType;\r\nimport java.lang.annotation.Retention;\r\nimport java.lang.annotation.RetentionPolicy;\r\nimport java.lang.annotation.Target;\r\n\r\n@Retention(RetentionPolicy.RUNTIME)\r\n@Target({ ElementType.FIELD })\r\npublic @interface OrderBy {\r\n \/**\r\n  * Field name\r\n  *\/\r\n String value();\r\n Order order() default Order.ASCENDING;\r\n SortPhase[] phase() default SortPhase.AFTER_CONVERT;\r\n}\r\n<\/pre>\n<p><strong>Implementing SortingMongoEventListener<\/strong>        <\/p>\n<p>Declarative sorting has to use reflection. To keep code readable I used <a href=\"http:\/\/commons.apache.org\/beanutils\/\">commons-beanutils<\/a> but it could have been done manually without using it. Add following dependencies to your project:         <\/p>\n<pre class=\"brush:xml\">&lt;dependency&gt;\r\n &lt;groupId&gt;commons-beanutils&lt;\/groupId&gt;\r\n &lt;artifactId&gt;commons-beanutils&lt;\/artifactId&gt;\r\n &lt;version&gt;1.8.3&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n\r\n&lt;dependency&gt;\r\n &lt;groupId&gt;commons-collections&lt;\/groupId&gt;\r\n &lt;artifactId&gt;commons-collections&lt;\/artifactId&gt;\r\n &lt;version&gt;3.2.1&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n<\/pre>\n<p>The final part is <code>SortingMongoEventListener<\/code> implementation:         <\/p>\n<pre class=\"brush:java\">package pl.maciejwalkowiak.springdata.mongodb;\r\n\r\nimport com.mongodb.DBObject;\r\nimport org.apache.commons.beanutils.BeanComparator;\r\nimport org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;\r\nimport org.springframework.data.mongodb.core.query.Order;\r\nimport org.springframework.util.ClassUtils;\r\nimport org.springframework.util.ReflectionUtils;\r\n\r\nimport java.lang.reflect.Field;\r\nimport java.util.Arrays;\r\nimport java.util.Collections;\r\nimport java.util.List;\r\n\r\n\/**\r\n * MongoEventListener that intercepts object before its converted to BasicDBObject (before it is saved into MongoDB)\r\n * and after its loaded from MongoDB.\r\n *\r\n * @author Maciej Walkowiak\r\n *\/\r\npublic class SortingMongoEventListener extends AbstractMongoEventListener {\r\n @Override\r\n public void onAfterConvert(DBObject dbo, final Object source) {\r\n  ReflectionUtils.doWithFields(source.getClass(), new SortingFieldCallback(source, SortPhase.AFTER_CONVERT));\r\n }\r\n\r\n @Override\r\n public void onBeforeConvert(Object source) {\r\n  ReflectionUtils.doWithFields(source.getClass(), new SortingFieldCallback(source, SortPhase.BEFORE_CONVERT));\r\n }\r\n\r\n \/**\r\n  * Performs sorting with field if:\r\n  * &lt;ul&gt;\r\n  * &lt;li&gt;field is an instance of list&lt;\/li&gt;\r\n  * &lt;li&gt;is annotated with OrderBy annotation&lt;\/li&gt;\r\n  * &lt;li&gt;OrderBy annotation is set to run in same phase as SortingFieldCallback&lt;\/li&gt;\r\n  * &lt;\/ul&gt;\r\n  *\/\r\n private static class SortingFieldCallback implements ReflectionUtils.FieldCallback {\r\n  private Object source;\r\n  private SortPhase phase;\r\n\r\n  private SortingFieldCallback(Object source, SortPhase phase) {\r\n   this.source = source;\r\n   this.phase = phase;\r\n  }\r\n\r\n  public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {\r\n   if (field.isAnnotationPresent(OrderBy.class)) {\r\n    OrderBy orderBy = field.getAnnotation(OrderBy.class);\r\n\r\n    if (Arrays.asList(orderBy.phase()).contains(phase)) {\r\n     ReflectionUtils.makeAccessible(field);\r\n     Object fieldValue = field.get(source);\r\n\r\n     sort(fieldValue, orderBy);\r\n    }\r\n   }\r\n  }\r\n\r\n  private void sort(Object fieldValue, OrderBy orderBy) {\r\n   if (ClassUtils.isAssignable(List.class, fieldValue.getClass())) {\r\n    final List list = (List) fieldValue;\r\n\r\n    if (orderBy.order() == Order.ASCENDING) {\r\n     Collections.sort(list, new BeanComparator(orderBy.value()));\r\n    } else {\r\n     Collections.sort(list, new BeanComparator(orderBy.value(), Collections.reverseOrder()));\r\n    }\r\n   }\r\n\r\n  }\r\n }\r\n}\r\n<\/pre>\n<p>In order to use it you just need to declare this class as a Spring bean in application context:         <\/p>\n<pre class=\"brush:xml\">&lt;bean class=\"pl.maciejwalkowiak.springdata.mongodb.SortingMongoEventListener\" \/&gt;\r\n<\/pre>\n<p><strong>Example<\/strong>        <\/p>\n<p>Now its time to add created OrderBy annotation to Backpack class from beginning of this post. Lets say we want to order items by price in descending order:         <\/p>\n<pre class=\"brush:java\">@Document\r\npublic class Backpack {\r\n @Id\r\n private ObjectId id;\r\n\r\n @OrderBy(value = \"price\", order = Order.DESCENDING)\r\n private List&lt;Item&gt; items;\r\n\r\n ...\r\n}\r\n<\/pre>\n<p>Thats it. Now every time you load Backpack objects \u2013 does not matter if its findAll, findOne or your custom method \u2013 items in backpack will be ordered.         <strong>&nbsp;<\/strong><\/p>\n<p><strong>Summary<\/strong>        <\/p>\n<p><code>SortingMongoEventListener<\/code> is another example how Spring Data MongoDB event system is powerful. You are welcome to comment and let me know if you think this feature could be a part of Spring Data MongoDB. <\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/maciejwalkowiak.pl\/blog\/2012\/05\/09\/sorting-spring-data-mongodb-collections-using-orderby\/\">Sorting Spring Data MongoDB collections using @OrderBy <\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Maciej Walkowiak at the <a href=\"http:\/\/maciejwalkowiak.pl\/\">Software Development Journey<\/a> blog.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is already third post about tuning and enhancing Spring Data MongoDB capabilities. This time I found that I miss one JPA feature \u2013 @OrderBy annotation. @OrderBy specifies the ordering of the elements of a collection valued association at the point when the association is retrieved. In this article I will show how to implement &hellip;<\/p>\n","protected":false},"author":245,"featured_media":187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[112,113,30,321],"class_list":["post-1533","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-mongodb","tag-nosql","tag-spring","tag-spring-data"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sorting Spring Data MongoDB collections using @OrderBy - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This is already third post about tuning and enhancing Spring Data MongoDB capabilities. This time I found that I miss one JPA feature \u2013 @OrderBy\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sorting Spring Data MongoDB collections using @OrderBy - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This is already third post about tuning and enhancing Spring Data MongoDB capabilities. This time I found that I miss one JPA feature \u2013 @OrderBy\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2012-07-14T15:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-22T06:01:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Maciej Walkowiak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Maciej Walkowiak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html\"},\"author\":{\"name\":\"Maciej Walkowiak\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/918c1b6d9d8de83830e5b24c0f0f4419\"},\"headline\":\"Sorting Spring Data MongoDB collections using @OrderBy\",\"datePublished\":\"2012-07-14T15:00:00+00:00\",\"dateModified\":\"2012-10-22T06:01:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html\"},\"wordCount\":434,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mongodb-logo.jpg\",\"keywords\":[\"MongoDB\",\"NoSQL\",\"Spring\",\"Spring Data\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html\",\"name\":\"Sorting Spring Data MongoDB collections using @OrderBy - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mongodb-logo.jpg\",\"datePublished\":\"2012-07-14T15:00:00+00:00\",\"dateModified\":\"2012-10-22T06:01:37+00:00\",\"description\":\"This is already third post about tuning and enhancing Spring Data MongoDB capabilities. This time I found that I miss one JPA feature \u2013 @OrderBy\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mongodb-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mongodb-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/07\\\/sorting-spring-data-mongodb-collections.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Sorting Spring Data MongoDB collections using @OrderBy\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/918c1b6d9d8de83830e5b24c0f0f4419\",\"name\":\"Maciej Walkowiak\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/897c85e1c7fe1b9b9ed640f6ff04c5f2141980d6c5c2b933034783495c8e8db5?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/897c85e1c7fe1b9b9ed640f6ff04c5f2141980d6c5c2b933034783495c8e8db5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/897c85e1c7fe1b9b9ed640f6ff04c5f2141980d6c5c2b933034783495c8e8db5?s=96&d=mm&r=g\",\"caption\":\"Maciej Walkowiak\"},\"sameAs\":[\"http:\\\/\\\/maciejwalkowiak.pl\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Maciej-Walkowiak\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sorting Spring Data MongoDB collections using @OrderBy - Java Code Geeks","description":"This is already third post about tuning and enhancing Spring Data MongoDB capabilities. This time I found that I miss one JPA feature \u2013 @OrderBy","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html","og_locale":"en_US","og_type":"article","og_title":"Sorting Spring Data MongoDB collections using @OrderBy - Java Code Geeks","og_description":"This is already third post about tuning and enhancing Spring Data MongoDB capabilities. This time I found that I miss one JPA feature \u2013 @OrderBy","og_url":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2012-07-14T15:00:00+00:00","article_modified_time":"2012-10-22T06:01:37+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","type":"image\/jpeg"}],"author":"Maciej Walkowiak","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Maciej Walkowiak","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html"},"author":{"name":"Maciej Walkowiak","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/918c1b6d9d8de83830e5b24c0f0f4419"},"headline":"Sorting Spring Data MongoDB collections using @OrderBy","datePublished":"2012-07-14T15:00:00+00:00","dateModified":"2012-10-22T06:01:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html"},"wordCount":434,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","keywords":["MongoDB","NoSQL","Spring","Spring Data"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html","url":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html","name":"Sorting Spring Data MongoDB collections using @OrderBy - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","datePublished":"2012-07-14T15:00:00+00:00","dateModified":"2012-10-22T06:01:37+00:00","description":"This is already third post about tuning and enhancing Spring Data MongoDB capabilities. This time I found that I miss one JPA feature \u2013 @OrderBy","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2012\/07\/sorting-spring-data-mongodb-collections.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Sorting Spring Data MongoDB collections using @OrderBy"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/918c1b6d9d8de83830e5b24c0f0f4419","name":"Maciej Walkowiak","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/897c85e1c7fe1b9b9ed640f6ff04c5f2141980d6c5c2b933034783495c8e8db5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/897c85e1c7fe1b9b9ed640f6ff04c5f2141980d6c5c2b933034783495c8e8db5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/897c85e1c7fe1b9b9ed640f6ff04c5f2141980d6c5c2b933034783495c8e8db5?s=96&d=mm&r=g","caption":"Maciej Walkowiak"},"sameAs":["http:\/\/maciejwalkowiak.pl"],"url":"https:\/\/www.javacodegeeks.com\/author\/Maciej-Walkowiak"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/1533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/245"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=1533"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/1533\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/187"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=1533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=1533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=1533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}