Top Banner
MongoDB with Spring Data Document Sean@Weaveus May 2011
16

Mongo db with spring data document

Sep 03, 2014

Download

Technology

Sean Lee

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mongo db with spring data document

MongoDB withSpring Data Document

Sean@WeaveusMay 2011

Page 2: Mongo db with spring data document

MongoDBDriver

MongoTemplate

MongoOperations

<<interface>>

Bean

Bean

@Autowired

2Monday, May 9, 2011

Page 3: Mongo db with spring data document

DB

Collection#1 Collection#2

Document#1

Document#2

Document#3

Document#4

Document#5

Document#1

Document#2

Document#3

Document#4

Document#5

Document := {UniqueID, JSON Document}

3Monday, May 9, 2011

Page 4: Mongo db with spring data document

Getting Started - Server

SeaniMac:tmp sean$ lsmongodb-osx-x86_64-1.8.1.tgzmongodb-osx-x86_64-1.8.1SeaniMac:tmp sean$ mkdir -p mongodb-data/dbSeaniMac:tmp sean$ ./mongodb-osx-x86_64-1.8.1/bin/mongod --dbpath ./mongodb-data/dbFri May! 6 14:16:48 [initandlisten] MongoDB starting ......

4Monday, May 9, 2011

Page 5: Mongo db with spring data document

Getting Started - Client

SeaniMac:tmp sean$ ./mongodb-osx-x86_64-1.8.1/bin/mongoMongoDB shell version: 1.8.1connecting to: test> db.foo.save( { a: 1 } )> db.foo.find(){ "_id" : ObjectId("4dc3855d3044bae59620330d"), "a" : 1 }

5Monday, May 9, 2011

Page 6: Mongo db with spring data document

Java(Groovy)import com.mongodb.Mongoimport com.mongodb.DBimport com.mongodb.DBCollection

Mongo m = new Mongo()println m.databaseNames

DB db = m.getDB("test")println db.collectionNames

DBCollection dbcol = db.getCollection("foo")println dbcol.find().next() // DBObject Instance, BSON

// results[test, admin, db, foo, local][foo, system.indexes][_id:4dc3855d3044bae59620330d, a:1.0]

6Monday, May 9, 2011

Page 7: Mongo db with spring data document

Document CRUD

7Monday, May 9, 2011

Page 8: Mongo db with spring data document

Save

> db.things.save({ name: "mongo" })> t = { x : 3 }{ "x" : 3 }> db.things.save(t)

8Monday, May 9, 2011

Page 9: Mongo db with spring data document

Find> db.things.find(){ "_id" : ObjectId("4dc399fc3044bae59620330e"), "name" : "mongo" }{ "_id" : ObjectId("4dc3a7913044bae59620330f"), "x" : 3 }> var cursor = db.things.find();> while (cursor.hasNext()) printjson(cursor.next());{ "_id" : ObjectId("4dc399fc3044bae59620330e"), "name" : "mongo" }{ "_id" : ObjectId("4dc3a7913044bae59620330f"), "x" : 3 }> db.things.find().forEach( printjson ){ "_id" : ObjectId("4dc399fc3044bae59620330e"), "name" : "mongo" }{ "_id" : ObjectId("4dc3a7913044bae59620330f"), "x" : 3 }

> db.things.find({name:"mongo"}).forEach( printjson );{ "_id" : ObjectId("4dc399fc3044bae59620330e"), "name" : "mongo" }

> db.things.save( {"name":"nosql", "title":"nonRDB"} )> db.things.find( {name:"nosql"}, {title:true} ).forEach( printjson ){ "_id" : ObjectId("4dc3ae133044bae596203312"), "title" : "nonRDB" }

9Monday, May 9, 2011

Page 10: Mongo db with spring data document

Remove

> db.things.remove({name:"mongo"})> db.things.remove({"_id":ObjectId("4dc3ae133044bae596203312")})> db.things.find()!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!{ "_id" : ObjectId("4dc3a7913044bae59620330f"), "x" : 3 }

10Monday, May 9, 2011

Page 11: Mongo db with spring data document

with Spring Data

11Monday, May 9, 2011

Page 12: Mongo db with spring data document

MongoTemplate

• Document databases extension to Spring Programming Model

• SPEL

• DataException hierarchy

• IoC Configuration

12Monday, May 9, 2011

Page 13: Mongo db with spring data document

Beans @Configuration or XML-Based

13Monday, May 9, 2011

Page 14: Mongo db with spring data document

MongoDBDriver

MongoTemplate

MongoOperations

<<interface>>

Bean

Bean

@AutowiredMongo

Repository

MongoCriteria

14Monday, May 9, 2011

Page 15: Mongo db with spring data document

Object-DocumentImplicit Conversion

15Monday, May 9, 2011

Page 16: Mongo db with spring data document

• MongoRepository

• Method name based Query

• @Query, QueryDSL

• Criteria, Predication Support

• GPS Query

• Mapping

• @Document(Instead of @Entity)

• Asynchronous Callback

16Monday, May 9, 2011