Intro to MongoDb

Preview:

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

Intro to MongoDB

In This Talk…

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

What is 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.

Not a RDBMS

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

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.

Mongo Document

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

}

Why Use 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)

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

Documents and Collections

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

To get an idea..

Embedded Objects

Documents can embed other documentsUsed to efficiently represent a relation

For example:

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

Querying

Queries

Queries are documentsCreate a collection

db.createCollection("users")

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

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]} } )

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()

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 } )

Queries

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

DELETE FROM usersdb.users.remove( )

DROP TABLE usersdb.users.drop()

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/

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

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

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

JavaScript Shell

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

Final Thoughts

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

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

Some Companies using MongoDB in Production

Questions?

Recommended