Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Data MongoDB

Spring Data MongoDB

Slides from Spring Data MongoDB speech given to Szczecin Java User Group on 20 of March 2013

Maciej Walkowiak

March 20, 2013
Tweet

More Decks by Maciej Walkowiak

Other Decks in Programming

Transcript

  1. post collection entry author collection entry { "_id" : ObjectId("5146227a0364038863be7514"),

    "title" : "post title", "content" : "hello jug", "createdAt" : ISODate("2013-03-17T20:07:22.455Z"), "tags" : [ "mongodb", "jug", "szczecin" ], } { "_id" : ObjectId("514622790364038863be7513"), "name" : "maciej walkowiak", }
  2. post collection entry linked with author { "_id" : ObjectId("5146227a0364038863be7514"),

    "title" : "post title", "content" : "hello world coders", "createdAt" : ISODate("2013-03-17T20:07:22.455Z"), "tags" : [ "mongodb", "jug", "szczecin" ], "author" : DBRef("author", ObjectId("514622790364038863be7513")) }
  3. post collection entry with embedded author { "_id" : ObjectId("51476f8d03646a6b3186f081"),

    "title" : "post title", "content" : "hello world coders", "createdAt" : ISODate("2013-03-18T19:48:29.667Z"), "tags" : [ "mongodb", "jug", "szczecin" ], "author" : { "name" : "maciej walkowiak", "email" : "[email protected]" } }
  4. db.post.find(); db.post.findOne({ _id: ObjectId("5146227a0364038863be7514") }); db.place.find({ coords : { $near:

    [-110,32], $maxDistance : 10/111.12 } }) db.post.find({ tags: "szczecin", createdAt: { $gt : ISODate("2013-03-01") } })
  5. Spring Data Same programming model for many databases simple things

    simple, complex things possible One API to rule them all - nope reduce amount boilerplate code
  6. Spring Data Same programming model for many databases simple things

    simple, complex things possible One API to rule them all - nope reduce amount boilerplate code
  7. Mapping { "_id" : ObjectId("5149471a0364c59b4e743584"), "_class" : "pl.maciejwalkowiak.jug.mongo.Post", "post_title" :

    "post title", "content" : "hello world coders", "createdAt" : ISODate("2013-03-20T05:20:26.985Z"), "comments" : [ ], "tags" : [ "mongodb", "jug", "szczecin" ], "version" : 0, "author" : DBRef("user", ObjectId("5149471a0364c59b4e743583")) }
  8. Mapping @Document public class Post { @Id private ObjectId id;

    @DBRef private User author; @Field("post_title") private String title; private String content; @CreatedDate private DateTime createdAt; private List<Comment> comments = new ArrayList<Comment>(); private Set<String> tags = new HashSet<String>(); @Version private Integer version; ... } { "_id" : ObjectId("5149471a0364c59b4e743584"), "_class" : "pl.maciejwalkowiak.jug.mongo.Post", "post_title" : "post title", "content" : "hello world coders", "createdAt" : ISODate("2013-03-20T05:20:26.985Z"), "comments" : [ ], "tags" : [ "mongodb", "jug", "szczecin" ], "version" : 0, "author" : DBRef("user", ObjectId("5149471a0364c59b4e743583")) }
  9. Template interface MongoOperations { <T> T findOne(Query query, Class<T> entityClass);

    <T> T findAndModify(Query query, Update update, Class<T> entityClass); long count(Query query, String collectionName); void save(Object objectToSave); WriteResult updateMulti(Query query, Update update, Class<?> entityClass); <T> void remove(Query query, Class<T> entityClass); <T> GroupByResults<T> group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass); <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, Class<T> entityClass); CommandResult executeCommand(DBObject command); ... }
  10. Repositories public interface UserRepository extends PagingAndSortingRepository<User, ObjectId>{ User findByName(String name);

    List<User> findByNameLike(String name, Pageable pageable); } public interface PostRepository extends CrudRepository<Post, ObjectId>, PostRepositoryCustom { @Query("{$where : 'this.content.length > 20'}") List<Post> findLongPosts(); @Query("{$where : 'this.content.length > ?0'}") List<Post> findPostsLongerThan(int length); }
  11. Repositories public interface UserRepository extends PagingAndSortingRepository<User, ObjectId>{ User findByName(String name);

    List<User> findByNameLike(String name, Pageable pageable); } @Repository public class PostRepositoryImpl implements PostRepositoryCustom { @Autowired private MongoOperations mongoOperations; public Post foo() { return mongoOperations.findOne(Query.query(Criteria.where("title").exists(true)), Post.class); public interface PostRepository extends CrudRepository<Post, ObjectId>, PostRepositoryCustom { @Query("{$where : 'this.content.length > 20'}") List<Post> findLongPosts(); @Query("{$where : 'this.content.length > ?0'}") List<Post> findPostsLongerThan(int length); }
  12. Map Reduce // reduce function (key, values) { var sum

    = 0; for (var i = 0; i < values.length; i++) sum += values[i]; return sum; } // map function () { emit(this.description, 1); }
  13. Map Reduce MapReduceResults<ValueObject> results = mongoOperations.mapReduce("collectionName", "classpath:map.js", "classpath:reduce.js", ValueObject.class); //

    reduce function (key, values) { var sum = 0; for (var i = 0; i < values.length; i++) sum += values[i]; return sum; } // map function () { emit(this.description, 1); }
  14. QueryDSL @Generated("com.mysema.query.codegen.EntitySerializer") public class QPost extends EntityPathBase<Post> { private static

    final long serialVersionUID = -1782980951; private static final PathInits INITS = PathInits.DIRECT; public static final QPost post = new QPost("post"); public final QUser author; public final ListPath<Comment, QComment> comments = this.<Comment, QComment>createList("comments", Comment.class, QComment.class, PathInits.DIRECT); public final StringPath content = createString("content"); public final org.joda.time.QDateTime createdAt; public final org.bson.types.QObjectId id; Type-Safe criteria API
  15. QueryDSL postRepository.findAll(QPost.post.content.contains("hello"))); @Generated("com.mysema.query.codegen.EntitySerializer") public class QPost extends EntityPathBase<Post> { private

    static final long serialVersionUID = -1782980951; private static final PathInits INITS = PathInits.DIRECT; public static final QPost post = new QPost("post"); public final QUser author; public final ListPath<Comment, QComment> comments = this.<Comment, QComment>createList("comments", Comment.class, QComment.class, PathInits.DIRECT); public final StringPath content = createString("content"); public final org.joda.time.QDateTime createdAt; public final org.bson.types.QObjectId id; Type-Safe criteria API
  16. Q&A