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”}]
})
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()
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))
#querying
event = Events.objects(title = “Title”)
#adding comments
event.update(push__comments = comment)