MongoDB using Grails Plugin By Puneet Behl
MongoDB using Grails Plugin By Puneet Behl
Agenda • Introduction to MongoDB – Understanding collections, documents. – Advantages of MongoDB over relational database. • Getting familiar with the MongoDB console. – Using selectors, inserts. – Updating document & update modifiers. – Upserts & Multiple Updates – Mastering Find • Integrating MongoDB with Grails using plug-in. – Exploring plug-in features. – Using Geospatial queries. – Dynamic attributes. – Using Low Level API.
Introduction to MongoDB • MongoDB (from “humongous”) is an open-source document database, and leading NoSQL database. • Written in C++.
What is MongoDB? • MongoDB is a document database that provides, high performance, high availability, and easy scalability.
Why MongoDB? “Show me your code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won’t usually need your code; it’ll be obvious.” - Eric Raymond, in The Cathedral and the Bazaar, 1997”
Why MongoDB? • Document Oriented Storage. • Full Index Support. • Replication & High Availability. • Auto-Sharding. • Querying. • Fast In-Place Updates. • Map/Reduce. • GridFS. • Professional Support By MongoDB.
Where MongoDB? • Big Data. • Content Management and Delivery. • Mobile and Social Infrastructure. • User Data Management. • Data Hub.
Customers
Collections & Documents • A MongoDB deployment hosts a number of databases. A database holds a set of collections. A collection holds a set of documents. A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.
Database • A physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.
Collection • A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection have a similar or related purpose.
Document • A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.
Advantages of MongoDB over Relational Databases • Schema-less. • Structure of a single object is clearer. • No sql or hibernate queries (complex joins). • Tuning • Ease of scale-out • Conversion mapping of application objects to database objects not needed. • Uses internal memory for storing the (windowed) working set, enabling faster access of data.
Getting Familiar with MongoDB console • Start mongod server. • db.help() • db.stats() • use <database-name> • db.dropDatabase() • db.getCollectionNames()
Commands: • Insert db.posts.insert({title: ‘MongoDB’, body: ‘I love MongoDB’, votes: 0}) • Find db.posts.find() //List all posts • Remove db.posts.remove() //Remove all posts
Insert db.posts.insert({ _id: ObjectId(-----(Timestamp),---(MachineID),--(ProcessID),---(Incrementer)) //Total 12(4 3 2 3) bytes, may be displayed in hex title: 'Big Data', body 'Dummy description about Big Data', tags: ['java', 'database', 'mongoDB', 'NoSQL'], votes: 100, comments: [ { by:'XYZ', text: 'Test comment 1', dateCreated: new Date(2013,2,25,6,45), votes: 0 }, { by:'ABC', text: 'Test comment 2', dateCreated: new Date(2013,2,25,7,45), votes: 5 } ] })
Special Operations • $lt : Less than • $lte: Less than Equals • $gt: Greater than • $gte: Greater than Equals • $ne: Not equals • Examples: – db.posts.find({tags: ‘java’,votes: {$gt:50}} – db.posts.find({title:{$ne: ‘Java’}, votes:{$gt:50}}) – db.posts.find({date:{$exists: true}})
AND, OR • {field1: value1, field2: value2} (AND statement) • $or: {field1: value1, field2:value2} • Examples: – db.posts.find({title: {$ne:’Windows’}, votes: {$gte:100}}) – db.posts.find(votes: {$gt:1000}, $or: [{tags: ‘java’}, {tags: ‘sun’}] })
Update & Update Modifiers • In the simplest form, update take 2 arguments: the selector (where) to use and what field to update with. • db.posts.update({title: ‘Big Data’},{‘votes’: 10000}) • db.posts.find({title:’Big Data’}) • db.posts.update({title:’Big Data’}, {$set: {tags: [‘MongoDB’, ’10gen’]}}) • db.posts.update({title: ‘Big Data’}, {$inc:{votes:-10}}) • db.posts.update({title: ‘Big Data’}, {$push: {tags:’NoSQL’}})
Upserts • An upsert updates the document if found or insert it if not. Upserts are handy to have in certain situations. To enable upserting set third parameter to true. • A mundane example for upsert is a hit counter for website. For example: – db.posts.update({title: ‘Test’}, {$inc: {hits: 1}}) – db.posts.find({title: ‘Test’},{hits:1,’_id’:0}) – db.posts.update({title: ‘Test’}, {$inc: {hits: 1}}, true) – db.posts.find({title: ‘Test’},{hits:1,’_id’:0})
Multiple Updates • For multiple updates, a fourth parameter must be set to true • db.posts.update({},{$inc: {votes: 10}} • db.posts.find() • db.posts.update({}.{$inc:{votes:10}}, false, true)
Mastering Find • Field Selection • Ordering • Paging • Count
Field Selection • Find takes a second optional parameter. This parameter is the list of fields we want to retrieve • By default _id field is always returned. We can explicitly exclude it. • Example: {name: 1. _id: 0} 1 for including and 0 for excluding the field.
Ordering • Find returns a cursor whose execution is delayed until needed. • We can chain sort method to find. • Sort works a lot like field selection. • Use 1 for ascending and -1 for descending order. • For example: – db.posts.find({}).sort({title:1}) – db.posts.find().sort({title:1, votes:-1})
Paging • Paging results can be accomplished via limit and skip cursor methods. • For example: – To get the post having highest votes we could do: • db.posts.find().sort({votes:-1}).limit(1) – To get the post having second highest votes: • db.posts.find().sort({votes:-1}).limit(2).skip(1)

MongoDB using Grails plugin by puneet behl

  • 1.
    MongoDB using GrailsPlugin By Puneet Behl
  • 2.
  • 3.
    Agenda • Introduction toMongoDB – Understanding collections, documents. – Advantages of MongoDB over relational database. • Getting familiar with the MongoDB console. – Using selectors, inserts. – Updating document & update modifiers. – Upserts & Multiple Updates – Mastering Find • Integrating MongoDB with Grails using plug-in. – Exploring plug-in features. – Using Geospatial queries. – Dynamic attributes. – Using Low Level API.
  • 4.
    Introduction to MongoDB •MongoDB (from “humongous”) is an open-source document database, and leading NoSQL database. • Written in C++.
  • 5.
    What is MongoDB? •MongoDB is a document database that provides, high performance, high availability, and easy scalability.
  • 6.
    Why MongoDB? “Show meyour code and conceal your data structures, and I shall continue to be mystified. Show me your data structures, and I won’t usually need your code; it’ll be obvious.” - Eric Raymond, in The Cathedral and the Bazaar, 1997”
  • 7.
    Why MongoDB? • DocumentOriented Storage. • Full Index Support. • Replication & High Availability. • Auto-Sharding. • Querying. • Fast In-Place Updates. • Map/Reduce. • GridFS. • Professional Support By MongoDB.
  • 8.
    Where MongoDB? • BigData. • Content Management and Delivery. • Mobile and Social Infrastructure. • User Data Management. • Data Hub.
  • 9.
  • 10.
    Collections & Documents •A MongoDB deployment hosts a number of databases. A database holds a set of collections. A collection holds a set of documents. A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.
  • 11.
    Database • A physicalcontainer for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.
  • 12.
    Collection • A groupingof MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection have a similar or related purpose.
  • 13.
    Document • A documentis a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.
  • 14.
    Advantages of MongoDBover Relational Databases • Schema-less. • Structure of a single object is clearer. • No sql or hibernate queries (complex joins). • Tuning • Ease of scale-out • Conversion mapping of application objects to database objects not needed. • Uses internal memory for storing the (windowed) working set, enabling faster access of data.
  • 15.
    Getting Familiar withMongoDB console • Start mongod server. • db.help() • db.stats() • use <database-name> • db.dropDatabase() • db.getCollectionNames()
  • 16.
    Commands: • Insert db.posts.insert({title: ‘MongoDB’,body: ‘I love MongoDB’, votes: 0}) • Find db.posts.find() //List all posts • Remove db.posts.remove() //Remove all posts
  • 17.
    Insert db.posts.insert({ _id: ObjectId(-----(Timestamp),---(MachineID),--(ProcessID),---(Incrementer)) //Total12(4 3 2 3) bytes, may be displayed in hex title: 'Big Data', body 'Dummy description about Big Data', tags: ['java', 'database', 'mongoDB', 'NoSQL'], votes: 100, comments: [ { by:'XYZ', text: 'Test comment 1', dateCreated: new Date(2013,2,25,6,45), votes: 0 }, { by:'ABC', text: 'Test comment 2', dateCreated: new Date(2013,2,25,7,45), votes: 5 } ] })
  • 18.
    Special Operations • $lt: Less than • $lte: Less than Equals • $gt: Greater than • $gte: Greater than Equals • $ne: Not equals • Examples: – db.posts.find({tags: ‘java’,votes: {$gt:50}} – db.posts.find({title:{$ne: ‘Java’}, votes:{$gt:50}}) – db.posts.find({date:{$exists: true}})
  • 19.
    AND, OR • {field1:value1, field2: value2} (AND statement) • $or: {field1: value1, field2:value2} • Examples: – db.posts.find({title: {$ne:’Windows’}, votes: {$gte:100}}) – db.posts.find(votes: {$gt:1000}, $or: [{tags: ‘java’}, {tags: ‘sun’}] })
  • 20.
    Update & UpdateModifiers • In the simplest form, update take 2 arguments: the selector (where) to use and what field to update with. • db.posts.update({title: ‘Big Data’},{‘votes’: 10000}) • db.posts.find({title:’Big Data’}) • db.posts.update({title:’Big Data’}, {$set: {tags: [‘MongoDB’, ’10gen’]}}) • db.posts.update({title: ‘Big Data’}, {$inc:{votes:-10}}) • db.posts.update({title: ‘Big Data’}, {$push: {tags:’NoSQL’}})
  • 21.
    Upserts • An upsertupdates the document if found or insert it if not. Upserts are handy to have in certain situations. To enable upserting set third parameter to true. • A mundane example for upsert is a hit counter for website. For example: – db.posts.update({title: ‘Test’}, {$inc: {hits: 1}}) – db.posts.find({title: ‘Test’},{hits:1,’_id’:0}) – db.posts.update({title: ‘Test’}, {$inc: {hits: 1}}, true) – db.posts.find({title: ‘Test’},{hits:1,’_id’:0})
  • 22.
    Multiple Updates • Formultiple updates, a fourth parameter must be set to true • db.posts.update({},{$inc: {votes: 10}} • db.posts.find() • db.posts.update({}.{$inc:{votes:10}}, false, true)
  • 23.
    Mastering Find • FieldSelection • Ordering • Paging • Count
  • 24.
    Field Selection • Findtakes a second optional parameter. This parameter is the list of fields we want to retrieve • By default _id field is always returned. We can explicitly exclude it. • Example: {name: 1. _id: 0} 1 for including and 0 for excluding the field.
  • 25.
    Ordering • Find returnsa cursor whose execution is delayed until needed. • We can chain sort method to find. • Sort works a lot like field selection. • Use 1 for ascending and -1 for descending order. • For example: – db.posts.find({}).sort({title:1}) – db.posts.find().sort({title:1, votes:-1})
  • 26.
    Paging • Paging resultscan be accomplished via limit and skip cursor methods. • For example: – To get the post having highest votes we could do: • db.posts.find().sort({votes:-1}).limit(1) – To get the post having second highest votes: • db.posts.find().sort({votes:-1}).limit(2).skip(1)