31
Intro to MongoDB

Intro to MongoDb

Embed Size (px)

DESCRIPTION

MongoDB (from "humongous") is a cross-platform document-oriented database system. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster. Released under a combination of the GNU Affero General Public License and the Apache License, MongoDB is free and open source software. First developed by the software company 10gen (now MongoDB Inc.) in October 2007 as a component of a planned platform as a service product, the company shifted to an open source development model in 2009, with 10gen offering commercial support and other services.[1] Since then, MongoDB has been adopted as backend software by a number of major websites and services, including Craigslist, eBay, Foursquare, SourceForge, and The New York Times, among others. MongoDB is the most popular NoSQL database system.[2]

Citation preview

Page 1: Intro to MongoDb

Intro to MongoDB

Page 2: Intro to MongoDb

In This Talk…

What is MongoDB?Why use it?Documents and CollectionsQueryingJavaScript ShellSchema Design

Page 3: Intro to MongoDb

What is MongoDB?

Page 4: Intro to MongoDb

What is MongoDB ?• Scalable High-Performance Open-source, Document-orientated database.

• Built for Speed• Rich Document based queries for Easy readability. • Full Index Support for High Performance. • Map / Reduce for Aggregation.• GridFS for supporting Large Binary Files.• Geospatial indexing for supporting Nearest values.• Replication and Failover for High Availability.• Auto Sharding for Easy Scalability.

Page 5: Intro to MongoDb

Not a RDBMS

Mongo is not a relational database like MySQLNo transactionsNo referential integrityNo joinsNo schema, so no columns or rowsNoSQL

Page 6: Intro to MongoDb

Document-oriented Database

Records are JSON documents (actually BSON)Stored in collectionsNo predefined schemaDocs in the same collection don’t even need to have the same fieldsAtomic in-place operators for contention-free updates

$set, $inc, $push, $pop, etc.

Page 7: Intro to MongoDb

Mongo Document

user = { name: "Frank Furter", occupation: "A scientist", location: "Transylvania"

}

Page 8: Intro to MongoDb

Why Use MongoDB?

Page 9: Intro to MongoDb

It’s Pretty PainlessIts fastSchemaless

No more configuring database columns with typesNo more defining and managing migrationsJust stick your data in there, it’s fine

NoSQLMongo’s query language is basically JSONThe Mongo driver for your favorite language is really nice and officially supportedHandy JavaScript shell for the CLI(Command-line interface)

Page 10: Intro to MongoDb

It’s Pretty Painless

/* First go create the database, the table, the schema, etc. */mysql_connect("localhost", "username", "password") or die(mysql_error());mysql_select_db("test") or die(mysql_error());$sql = "INSERT INTO users (name, age) VALUES ('Janet', 23)";mysql_query($sql);$result = mysql_query("SELECT * FROM users WHERE age = 23");$row = mysql_fetch_assoc($result);echo "Oh, " . $row['name'] . "!"; // prints "Oh, Janet!"

$mongo = new Mongo(); // defaults to localhost with no auth$users = $mongo->test_db->users; // database and collection created implicitly$users->insert( array('name' => 'Brad', 'age' => 25) );$user = $users->findOne( array('age' => 25) );echo "Oh, " . $user->name . "!"; // prints "Oh, Brad!"

MySQL

MongoDB

Page 11: Intro to MongoDb

Documents and Collections

Page 12: Intro to MongoDb

Documents and Collections

Documents are the recordsLike objects in OOP, or rows in RDBMS

Collections are groups of documentsUsually represent a top-level class in your appHeterogeneous setUnlike RDBMS tables, no predefined schema

No foreign keys, so how do we reference other objects?

Just embed the sub-item in the parent docOr, use a key for references and deal with the fact that you don't get integrity or joins

Page 13: Intro to MongoDb

To get an idea..

Page 14: Intro to MongoDb

Embedded Objects

Documents can embed other documentsUsed to efficiently represent a relation

For example:

{ name: 'Brad Majors', address: { street: 'Oak Terrace', city: 'Denton' }}

Page 15: Intro to MongoDb

Querying

Page 16: Intro to MongoDb

Queries

Queries are documentsCreate a collection

db.createCollection("users")

To Add a documentsdb.users.insert( { user_id: "001", age: 23, status: "A“ , name : “Smith” } )

Page 17: Intro to MongoDb

Queries

Queries are documentsQuery expression objects indicate a pattern to match

db.users.find( {last_name: 'Smith'} )

Several query objects for advanced queriesWhere age > 23db.users.find( {age: {$gt: 23} } )db.users.find( {age: {$in: [23,25]} } )

Page 18: Intro to MongoDb

Queries

SELECT COUNT(*) FROM users db.users.count()

SELECT DISTINCT(status) FROM users db.users.distinct( "status" )

SELECT * FROM users LIMIT 1db.users.find().limit(1)

EXPLAIN SELECT * FROM users WHERE status = "A“

db.users.find( { status: "A" } ).explain()

Page 19: Intro to MongoDb

Queries

UPDATE users SET status = "C" WHERE age > 25db.users.update( { age: { $gt: 25 } }, { $set: { status: "C" } }, { multi: true } )

UPDATE users SET age = age + 3 WHERE status = "A“

db.users.update( { status: "A" } , { $inc: { age: 3 } }, { multi: true } )

Page 20: Intro to MongoDb

Queries

DELETE FROM users WHERE status = "D“db.users.remove( { status: "D" } )

DELETE FROM usersdb.users.remove( )

DROP TABLE usersdb.users.drop()

Page 21: Intro to MongoDb

Querying Embedded Objects

Exact match an entire embedded objectdb.users.find( {address: {street: 'Oak Terrace', city: 'Denton'}} )

Dot-notation for a partial matchdb.users.find( {"address.city": 'Denton'} )

http://docs.mongodb.org/manual/reference/sql-comparison/

Page 22: Intro to MongoDb

Managing large binary dataDivide a large file among multiples documents (GridFS)Include metadata to large files Search files base on its contentRetrieve only the first N bytes of a video

Relational databaseUse BLOB (Binary large objects)Inefficient manipulating rich media BLOB cannot be searched or manipulated using standard database command

22

Page 23: Intro to MongoDb

Geospatial Indexes

Queries to find the nearest N point to a current locationMongoDB

Embedded Geospatial featuresRelational database

Spatial extensionsMySQL implements a subset of the SQL with Geometry Types environment proposed by Open Geospatial Consortium (OGC)

23

Page 24: Intro to MongoDb

Managing huge volume of dataMongoDB

High performance No joins and embedding makes reads and writes fastIndexes including indexing of keys from embedded documents and arrays

Horizontal scalabilityAutomatic sharding (auto-partitioning of data across servers)

Relational databaseHave shown poor performance on certain data-intensive applications and delivering streaming media Case study: FoursquareDifficult to scale to multiple servers

24

Page 25: Intro to MongoDb

JavaScript Shell

Page 26: Intro to MongoDb

JS Shell

Comes with MongoDBLaunch it with 'mongo' on the command-lineTry a simplified version at http://www.mongodb.org/Great fit since Mongo docs are basically JSON

Page 27: Intro to MongoDb

Final Thoughts

Page 28: Intro to MongoDb

Final Thoughts

MongoDB is fast no matter how you slice itIt achieves high performance by literally playing fast and loose with your dataVery rapid development, open sourceDocument model is simple but powerfulAdvanced features like map/reduce, geospatial indexing etc. are very compellingSurprisingly great drivers for most languages

Page 29: Intro to MongoDb

When should you use something else?

Applications that require SQL.Applications with a heavy emphasis on complex transactionsTraditional Data WarehousingThe application requires a good amount of reportingYour application is awkwardly write heavy

Page 30: Intro to MongoDb

Some Companies using MongoDB in Production

Page 31: Intro to MongoDb

Questions?