The Hows and Whys of MongoDB

51
The Hows and Whys of MongoDB 27 May 2014

description

Want to get started with MongoDB? Here is a quick intro the major features of the most popular NoSQL database.

Transcript of The Hows and Whys of MongoDB

Page 1: The Hows and Whys of MongoDB

The Hows and Whys of MongoDB

27 May 2014

Page 2: The Hows and Whys of MongoDB

Everything is at: http://bitly.com/bundles/rockncoder/2

Page 3: The Hows and Whys of MongoDB

Who am I?Hi, I am Troy. I have fun as a full stack programmer. I develop using ASP.NET MVC or Node.js on the backend and the web or mobile up front. !

I can be reached at: [email protected]

Page 4: The Hows and Whys of MongoDB

Free mobile tutorials with source code @ therockncoder.blogspot.com

Page 5: The Hows and Whys of MongoDB

Want more? Follow me, new tutorials are announced on Twitter first:

@therockncoder

Page 6: The Hows and Whys of MongoDB

Source code for my tutorials hosted on GitHub @

https://github.com/Rockncoder

Page 7: The Hows and Whys of MongoDB

Check out my videos: www.youtube.com/rockncoder

Page 8: The Hows and Whys of MongoDB

MongoDB 9 to 5

• Two day intensive training

• July 19th & 26th

• 9 AM - 5 PM

• ITT Technical Institute, Torrance, CA

Page 9: The Hows and Whys of MongoDB

Agenda• Introduction to

MongoDB

• The MongoDB Shell

• Using MongoDB

• Advanced MongoDB

• MongoDB Tools

• To MongoDB or not to MongoDB

• Summary

• Questions

Page 10: The Hows and Whys of MongoDB

Introduction to MongoDB

Page 11: The Hows and Whys of MongoDB

What is MongoDB?

• Cross-platform document database

• Developed by MongoDB Inc in 2007

• Production ready since March 2010

• Free and open-source

• The most popular NoSQL database

Page 12: The Hows and Whys of MongoDB

Top DB-Engines• Oracle

• MySQL

• MS SQL Server

• PostgreSQL

• MongoDB

!

• DB2

• MS Access

• SQLite

• Cassandra

• Sybase ASE

Page 13: The Hows and Whys of MongoDB

Who Uses It?• Craigslist

• eBay

• Foursquare

• SourceForge

• Viacom

• Expedia

• Parse

• LinkedIn

• Medtronic

• eHarmony

• CERN

• and more

Page 14: The Hows and Whys of MongoDB

Why?

• Document Database

• High Performance

• High Availability

• Easy Scalability

Page 15: The Hows and Whys of MongoDB

What is a Document?

• An ordered set of keys and values

• like JavaScript objects

• no duplicate keys allowed

• type and case sensitive

• field order is not important or guaranteed

Page 16: The Hows and Whys of MongoDB

What’s Wrong with SQL?

• SQL was created by Edgar F. Codd in 1969

• Oracle V2 introduced in 1979

• MS SQL Server introduced in 1989

• Today most languages are object-oriented

• SQL is column and row oriented

Page 17: The Hows and Whys of MongoDB

A Contact Manager

• first name

• last name

• home address

• work address

• mobile phone

• home phone

Page 18: The Hows and Whys of MongoDB

In SQL

• Person table

• Address table

• Phone number table

• Joins necessary to retrieve

Page 19: The Hows and Whys of MongoDB

In MongoDB

• One document holds it all

• Including the arrays

• No joins necessary

Page 20: The Hows and Whys of MongoDB

SQL MongoDB

row document

table collection

database database

joins none

transactions none

Page 21: The Hows and Whys of MongoDB

Installation

• The current version is 2.6.1

• Downloads available for Windows, Linux, Mac OSX, and Solaris

• 64-bit for all systems, 32-bit for Windows & Linux

• 32-bit is not recommends except for learning

Page 22: The Hows and Whys of MongoDB

Windows

• Download MongoDB

• Unzip the file

• Move it, if necessary

• Set up the MongoDB environment

• Start MongoDB

• Connect to MongoDB

Page 23: The Hows and Whys of MongoDB

Mac OS X

• The best way is to use Homebrew

• brew update

• brew install mongo

Page 24: The Hows and Whys of MongoDB

The MongoDB Shell

Page 25: The Hows and Whys of MongoDB

The MongoDB Shell

• Allows interactions with a MongoDB instance

• A full-featured JavaScript interpreter

• Allows multiple line input

Page 26: The Hows and Whys of MongoDB

Shell Helpers

• use foo - db.getSisterDB(“foo”)

• show dbs - db.getMongo().getDBs()

• show collections - db.getCollectionNames()

Page 27: The Hows and Whys of MongoDB

CRUD in the Shell

• Create = insert

• Read = find/findOne

• Update = update

• Delete = remove

Page 28: The Hows and Whys of MongoDB

BSON not JSON

• MongoDB uses its own variant of JSON

• Called Binary JSON or BSON

• Efficiency

• Traversability

• Performance

Page 29: The Hows and Whys of MongoDB

Using MongoDB

Page 30: The Hows and Whys of MongoDB

MongoDB in Node.js

Page 31: The Hows and Whys of MongoDB

MongoDB in Android

Page 32: The Hows and Whys of MongoDB

MongoDB in C#

Page 33: The Hows and Whys of MongoDB

Misunderstandings

• It is schema-less

• You don’t need to design db

• You should mix types

Page 34: The Hows and Whys of MongoDB

Advanced MongoDB

Page 35: The Hows and Whys of MongoDB

Performance

• Indexing

• Query Optimization

• Profiler

Page 36: The Hows and Whys of MongoDB

Indexing

• Indexes should support queries

• Use indexes to sort queries

• Indexes should fit into RAM

• Queries should ensure selectivity

Page 37: The Hows and Whys of MongoDB

Query Optimization

• Improves read operations by reducing data that the query needs to process

Page 38: The Hows and Whys of MongoDB

Profiler

• Collects data about MongoDB database commands

• Enabled per-database or per-instance basis

• Profile level is configurable (0, 1, or 2)

Page 39: The Hows and Whys of MongoDB

Stats

• db.stats()

• Statistics that reflect the use of a single DB

• Identifies:

• the current database

• the number of indexes

• the file size

Page 40: The Hows and Whys of MongoDB

Replication

• Keeps identical copies of data on multiple servers

• Set up by creating a replica set

• A replica set is a group of servers with one primary

• If primary crash, secondaries elect a new one

Page 41: The Hows and Whys of MongoDB

Backup

• mongodump - command line tool

• You must have backup and read privileges

• No arguments connects to the local DB

• —db, limits the database

• —collection, limits the collections

• —oplog, makes snapshots possible

Page 42: The Hows and Whys of MongoDB

Restore

• mongorestore --port <port number> <path to the backup>

• Can restore to a running instance or to the files

• —oplogReplay,

• —filter, allows restore only if filter is true

Page 43: The Hows and Whys of MongoDB

Sharding

• Process of splitting data up across machines

• Manual sharding can be with most database

• MongoDB has autosharding

• Nonetheless it is difficult to configure

Page 44: The Hows and Whys of MongoDB

MongoDB Tools

Page 45: The Hows and Whys of MongoDB

Tools

• MongoDB Shell (built-in)

• MongoDB Web Site (built-in)

• Robomongo (Mac, PC, Linux)

• http://mongodb-tools.com/

Page 46: The Hows and Whys of MongoDB

To MongoDB or not to MongoDB

Page 47: The Hows and Whys of MongoDB

Not to MongoDB

• Transactions are needed

• You are happy with what you have

• You have have

Page 48: The Hows and Whys of MongoDB

To MongoDB

• You do a lot of non-transactional writes

• You need to grow wide

• You work with location based data

• You need high availability

Page 49: The Hows and Whys of MongoDB

MongoDB as a Service

• MongoHQ

• MongoLab

• MongoDirector

• ObjectRocket, (Rackspace)

Page 50: The Hows and Whys of MongoDB

Summary

• MongoDB is an open-source document database

• It features JSON-style documents with dynamic schemas

• In order to gain performance, it sacrifices reliability

Page 51: The Hows and Whys of MongoDB

Everything is at: http://bitly.com/bundles/rockncoder/2