- MongoDB is a non-relational, document-oriented database that scales horizontally and uses JSON-like documents with dynamic schemas.
- It supports complex queries, embedded documents and arrays, and aggregation and MapReduce for querying and transforming data.
- MongoDB is used by many large companies for operational databases and analytics due to its scalability, flexibility, and performance.
5. JSON-style Documents { hello : “world” } 16000000 02 hello00 06000000world00 00 http://bsonspec.org represented as BSON Just like a light and friendly XML
6. Flexible “Schemas” In collection db.posts: {author: “mike”, links: 3 , date: "Sun Jul 18 2010 14:40:20 GMT-0700 (PDT)" text: “blah blah”} {author: “eliot”, date: "Sun Jul 18 2010 14:40:22 GMT-0700 (PDT)" text: “Here is MongoDB ...”, views: 10 } Potentially all documents in the same collection
7. Embedded Document { _id: ObjectId("4d1009c7262bb4b94af1cea4") author_id: “1346”, date: "Sun Jul 18 2010 14:40:20 GMT-0700 (PDT)", title: “my story” text: “once upon a time ...”, tags: [“novel”,”english”], Comments:[ {user_id: 234, text: “awesome dude”}, {user_id: 1235, text: “that made me cry”}] } little need for joins or transactions across documents!
8. Data Model Normalized: Many tables Row/Column Natural: Collections Documents Just BLOBS Can't scale As fast as BLOBs if well modeled, horizontal scalability Easy to scale (Dynamo), can easily use caching No feature Most regular SQL features (satisfies 90% of users) (too) many PERFORMANCE FEATURES
9.
10. Replication master slave slave Using Replica Set: - pool of servers with 1 master - automatic master election and failover - distributed reads (slaveOk) slave Client Client
11. Sharding client mongos ... mongos mongod ... Shards mongod mongod mongod Config Servers mongod mongod mongod mongod mongod mongod mongod mongod ... For large datasets, or write heavy system
12.
13.
14. New Post > post = {author: "mike", ... date: new Date(), ... text: "my blog post", ... tags: ["mongodb", "intro"]} > db.posts.save(post) > db.posts.findOne() { "_id" : ObjectId("4d2f944103e8fdbb36f6d205"), "author" : "mike", "date" : ISODate("2011-01-14T00:08:49.933Z"), "text" : "my blog post", "tags" : ["mongodb","intro"]}
18. More Querying Find by Author > db.posts.find({author: "tony"}) 10 most recent posts: > db.posts.find().sort({date: -1}).limit(10) Posts since April 1 st : > april_1 = new Date(2010, 3, 1) > db.posts.find({date: {$gt: april_1}}) Adding an index to speed up: > db.posts.ensureIndex({author: 1}) > db.posts.ensureIndex({date: 1})
19. More Querying Find with regexp: > db.posts.find({text: /post$/}) Find within array: > db.posts.find({tags: "intro"}) > db.posts.ensureIndex({tags: 1}) Find within embedded object: > db.posts.find({"comments.author": "eliot"}) > db.posts.ensureIndex({"comments.author": 1})