Your SlideShare is downloading. ×
  • Like
Starting with MongoDB
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Starting with MongoDB

  • 1,158 views
Published

Talk by Murad Kamalov from DoThinger Team at the Codecraft meeting (Dunedin, New Zealand).

Talk by Murad Kamalov from DoThinger Team at the Codecraft meeting (Dunedin, New Zealand).

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,158
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
22
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Starting with MongoDB Murad Kamalov
  • 2. EstoniaTallinn Tartu
  • 3. FinlandHelsinki
  • 4. Dunedin
  • 5. DoThinger.com @DoThinger• Do things together with people nearby
  • 6. Intro to MongoDB• Schemaless• Supports replication and shardling• Indexing• File storage• Aggregation (Map/Reduce)• Geospacial queries
  • 7. Benefits of MongoDB• Great choice for agile projects – no schema = no limitations – ease of use• Scalability & high availability – thanks to shardling and replication• Failover and automatic recovery
  • 8. Main Concepts• Database is set of collections• Collection is like a table in MySQL (e.g events collection)• Document belongs to a collection, like row in MySQL, unlike in MySQL documents in same collection can have different structure• Documents store fields as key-value pairs, where value can be basic data type, another document or array• Data is stored in JSON format (serialized as BSON)
  • 9. Example• Inserting document into events collection: > db.events.insert({ “title” : “CodeCraft Meeting”, “description” : “CodeCraft meeting in Dunedin on Tuesday evening”, “start_datetime” : ISODate(“2012-04-03T17:30:00”), “category” : “social” }) > db.events.insert({ “title” : “CodeCraft Meeting 2”, “description” : “CodeCraft meeting in Dunedin on Tuesday evening”, “start_datetime” : ISODate(“2012-05-03 17:30:00”), “category” : “social”, “participants” : 50 })• When Inserting, indexed field _id is created automatically for each inserted document
  • 10. Embedded Documents• Linking versus Embedding – e.g instead of creating separate collection for comments, just embed comments in events collection – MongoDB doesn’t support JOINs = use embedding> db.events.insert({ “title” : “Title”, “description” : “Description”, “start_date” : ISODate(“2012-04-03T17:30:00”), “category” : “social”, “comments”: [ {“author”: <user_id>, “comment” :“My First Comment”}, {“author”: <user_id>, “comment”: “My Second Comment”}] })
  • 11. Querying• Can be done in many various ways in MongoDB> db.events.find({“title”: “CodeCraft Meeting”})> {“_id”: ObjectId(41234351451345), “title” : “CodeCraft Meeting”, “description” : “Description”, “start_datetime” : ISODate(“2012-04-03T17:30:00”), “category” : “social”}> db.events.find({“start_datetime” : {$gte : ISODate(“2012- 04-01T00:00:00”)}})> {“_id”: ObjectId(41234351451345), “title” : “CodeCraft Meeting”, “description” : “Description”, “start_datetime” : ISODate(“2012-04-03T17:30:00”), “category” : “social”} {“_id”: ObjectId(41226542345234), “title” : “CodeCraft Meeting 2”, “description” : “Description”, “start_datetime” : ISODate(“2012-05-03T17:30:00”), “category” : “social”, “participants” : 50}
  • 12. Updates• Replacing the entire document> db.events.update({“_id”: ObjectId(41234351451345)}, {“title” : “New Title”, “description” : “Description”, “start_date” : ISODate(“2012-04-03T17:30:00”), “category” : “social”, “comments”: [ {“author”: <user_id>, “comment” :“My First Comment”}, {“author”: <user_id>, “comment”: “My Second Comment” })• Atomic updates> db.events.update({“_id”:ObjectId(41234351451345)}, {$set : {“title”: {“New Title”}}} )
  • 13. Updates (2)• Pushing Elements to Arrays> db.events.update({“_id”:ObjectId(41234351451345)}, {$push : {“comments”: {“author”: <user_id>, “comment”: “My Third Comment”}}})> {“title” : “Title”, “description” : “Description”, “start_date” : ISODate(“2012-04-03T17:30:00”), “category” : “social”, “comments”: [ {“author”: <user_id>, “comment” :“My First Comment”}, {“author”: <user_id>, “comment”: “My Second Comment”}, {“author”: <user_id>, “comment”: “My Third Comment”}] })
  • 14. Geospatial queries• MongoDB has build-in support for geospacial queries> db.events.insert({ “title” : “Title”, “description” : “Description”, “start_datetime” : ISODate(“2012-04-03T17:30:00”), “category” : “social”, “location” : [30, 30] })> db.events.ensureIndex({“location” : “2d”})> db.events.find({“location”: {$near: [28,28], $maxDistance: 5}})
  • 15. Files (GridFS)• In MySQL it’s a bad practice to save large binary files into the database• In MongoDB it’s a bad practice NOT TO save large files into the database.• MongoDB splits saved files into chunks, which allows querying of only necessary parts of the binary files• Example(pymongo library) >>> fs = gridfs.GridFs(db_name) >>> filename = fs.put(“Example file data”) >>> file_data = fs.get(filename).read()
  • 16. Map/Reducedb.events.mapReduce(map_func, reduce_func,{out: “out_collection”})• map_func and reduce_func are written in JavaScript• Output of reduce_func is stored in output collection
  • 17. Language Drivers• Different language drivers for MongoDB provide different levels of abstraction – PyMongo (similar to mongo shell) vs MongoEngine (ORM-like)class Event(mongoengine.Document): title = StringField() description = StringField() start_date = DateTimeField(default=datetime.now) category = StringField() comments = ListField(EmbeddedDocumentField(Comment))#queryingevent = Events.objects(title = “Title”)#adding commentsevent.update(push__comments = comment)
  • 18. Thank You! Questions?