The Hows and Whys of MongoDB
-
Upload
troy-miles -
Category
Software
-
view
131 -
download
1
description
Transcript of The Hows and Whys of MongoDB
The Hows and Whys of MongoDB
27 May 2014
Everything is at: http://bitly.com/bundles/rockncoder/2
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]
Free mobile tutorials with source code @ therockncoder.blogspot.com
Want more? Follow me, new tutorials are announced on Twitter first:
@therockncoder
Source code for my tutorials hosted on GitHub @
https://github.com/Rockncoder
Check out my videos: www.youtube.com/rockncoder
MongoDB 9 to 5
• Two day intensive training
• July 19th & 26th
• 9 AM - 5 PM
• ITT Technical Institute, Torrance, CA
Agenda• Introduction to
MongoDB
• The MongoDB Shell
• Using MongoDB
• Advanced MongoDB
• MongoDB Tools
• To MongoDB or not to MongoDB
• Summary
• Questions
Introduction to 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
Top DB-Engines• Oracle
• MySQL
• MS SQL Server
• PostgreSQL
• MongoDB
!
• DB2
• MS Access
• SQLite
• Cassandra
• Sybase ASE
Who Uses It?• Craigslist
• eBay
• Foursquare
• SourceForge
• Viacom
• Expedia
• Parse
• Medtronic
• eHarmony
• CERN
• and more
Why?
• Document Database
• High Performance
• High Availability
• Easy Scalability
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
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
A Contact Manager
• first name
• last name
• home address
• work address
• mobile phone
• home phone
In SQL
• Person table
• Address table
• Phone number table
• Joins necessary to retrieve
In MongoDB
• One document holds it all
• Including the arrays
• No joins necessary
SQL MongoDB
row document
table collection
database database
joins none
transactions none
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
Windows
• Download MongoDB
• Unzip the file
• Move it, if necessary
• Set up the MongoDB environment
• Start MongoDB
• Connect to MongoDB
Mac OS X
• The best way is to use Homebrew
• brew update
• brew install mongo
The MongoDB Shell
The MongoDB Shell
• Allows interactions with a MongoDB instance
• A full-featured JavaScript interpreter
• Allows multiple line input
Shell Helpers
• use foo - db.getSisterDB(“foo”)
• show dbs - db.getMongo().getDBs()
• show collections - db.getCollectionNames()
CRUD in the Shell
• Create = insert
• Read = find/findOne
• Update = update
• Delete = remove
BSON not JSON
• MongoDB uses its own variant of JSON
• Called Binary JSON or BSON
• Efficiency
• Traversability
• Performance
Using MongoDB
MongoDB in Node.js
MongoDB in Android
MongoDB in C#
Misunderstandings
• It is schema-less
• You don’t need to design db
• You should mix types
Advanced MongoDB
Performance
• Indexing
• Query Optimization
• Profiler
Indexing
• Indexes should support queries
• Use indexes to sort queries
• Indexes should fit into RAM
• Queries should ensure selectivity
Query Optimization
• Improves read operations by reducing data that the query needs to process
Profiler
• Collects data about MongoDB database commands
• Enabled per-database or per-instance basis
• Profile level is configurable (0, 1, or 2)
Stats
• db.stats()
• Statistics that reflect the use of a single DB
• Identifies:
• the current database
• the number of indexes
• the file size
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
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
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
Sharding
• Process of splitting data up across machines
• Manual sharding can be with most database
• MongoDB has autosharding
• Nonetheless it is difficult to configure
MongoDB Tools
Tools
• MongoDB Shell (built-in)
• MongoDB Web Site (built-in)
• Robomongo (Mac, PC, Linux)
• http://mongodb-tools.com/
To MongoDB or not to MongoDB
Not to MongoDB
• Transactions are needed
• You are happy with what you have
• You have have
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
MongoDB as a Service
• MongoHQ
• MongoLab
• MongoDirector
• ObjectRocket, (Rackspace)
Summary
• MongoDB is an open-source document database
• It features JSON-style documents with dynamic schemas
• In order to gain performance, it sacrifices reliability
Everything is at: http://bitly.com/bundles/rockncoder/2