In this workshop we will deploy a pre-built Node website to Heroku, then hook it up to an mLabs MongoDB instance. We will then use both the Mongo Shell and a GUI based app to import and export data, save and modify documents, and run queries. Finally, we'll use our knowledge of Mongo queries to create a RESTful api for the Node app.
This is a workshop designed for experienced JavaScript developers. You must already be familiar with the following: JavaScript, Git, using a programming editor, running commands from the terminal, and launching a web server on your own machine.
5. Troy Miles
Troy Miles aka the RocknCoder
Over 36 years of programming experience
Author of jQuery Essentials
Speaker and writer on all things web & mobile
rockncoder@gmail.com
@therockncoder
6. Agenda
Introduction to MongoDB
Installing MongoDB
Riding the BSON
Enter the Mongo (part 1)
BREAK
The Other Mongo Apps
Importing Data
LUNCH
Enter the Mongo (part 2)
Indexing
BREAK
8. How to get the most out of
this class
Follow along
Complete each exercise
Ask questions and for clarification
Don’t worry about asking questions, I like them
Use what you’ve learned after the class is over
10. What is MongoDB?
Cross-platform document database
Developed by MongoDB Inc in 2007
Production ready since March 2010
Free and open-source
The current version is 2.6.4
11. Top DB-Engines
1. Oracle
2. MySQL
3. MS SQL Server
4. PostgreSQL
5. MongoDB
6. DB2
7. MS Access
8. SQLite
9. Cassandra
10.Sybase ASE
14. 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 nor guaranteed
15. MongoDB Myths
It is schema-less
You don’t need to design db
You can mix types, therefore you should
16. 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
Most languages today are object-oriented
SQL is column and row oriented
24. Installation
The current version is 3.2
Downloads available for Windows, Linux, Mac OSX,
and Solaris
64-bit for all systems, 32-bit for Windows & Linux
32-bit is not recommended
25. Windows
Determine your machine’s architecture (64 or 32 bit)
wmic os get osarchitecture
Download & run the MongoDB msi file
Create the data directory
md datadb
Run MongoDB from a command window
mongod
26. Mac OS X
Open Terminal
Extract the archive
tar -zxvf mongodb-osx-x86_64-2.6.3.tgz
Copy the files to the target directory
mkdir -p mongodb
cp -R -n mongodb-osx-x86_64-2.6.3/ mongodb
27. Mac OS X
Create the data directory
mkdir -p /data/db
Run MongoDB
mongod
33. Object
An unordered set of name/value pairs
The name is any valid string
The name may be unquoted if it is not a reserved
The value may be any of the other valid types including
another object or an array
34. Array
An ordered collection of values
Begins and ends with square brackets
The values can be any of the other types
35. Binary data aka BinData
Used to represent arrays of bytes
Somewhat like a ByteArray in Java or byte[] in C#
Can be used in equal comparisons
Other comparisons exist but might produce some
weird results
36. Object id
12-byte BSON type
4 bytes for the number of seconds since Jan 1, 1970
3 bytes for the machine id
2 bytes for the process id
3 bytes for a counter which begins with a random
37. Date
64 bit Integer
Not related to the Timestamp type
Represents the number of milliseconds since 1 January
1970
Date has a time range of plus or minus 290 million years
No chance of a Y2K problem anytime soon
38. Timestamp
64 bit Integer
Not related to the Date type
First 32 bits represents the seconds since Jan 1, 1970
Second 32 bits is an incrementing ordinal
Within a mongod instance timestamps are unique
39. Regular Expression (Reg Ex)
A string representing a JavaScript style regular
expression
Regular expressions give MongoDB the ability to do
something like SQL LIKE operation
40. JavaScript
A string representing a JavaScript program
The program can be stored with or without scope
Care should be used in executing JavaScript on the
server side since it operates in the JavaScript shell
41. Limits
A document has a max size of 16 MB
Documents can’t be nest any more than a 100 levels
The namespace which includes the database and
collection names must be shorter than 123
No single collection can have more than 64 indexes
42. Section Summary
MongoDB has all of the expected data types
And a few surprising ones
Exercise caution when JavaScript type
45. The MongoDB Shell
Allows interactions with a MongoDB instance
A full-featured JavaScript interpreter
Allows multiple line input
46. Enter the Mongo
From the command or terminal window enter mongo
and press enter
You should see a greater than sign “>”
You can exit the shell by entering exit
47. Commands
Mongo has a lot of commands
Some are global and operate against the environment
Others refer only to databases and are preceded by db.
There also commands which refer to a collection, they
db.<Collection Name>.<command>
The most important command is help
48. Creating a database
To make sure we are all on the same page we are
going to create a database named “m95”
To create a database Enter: use m95
If you want to delete a database, you need to drop it,
Enter db.dropDatabase()
Be careful there is no confirmation
49. Inserting Documents
Having a database is nice, nicer still is having data in it
To insert data, db.<Collection Name>.insert(…)
Don’t worry about the collection name, if it doesn’t
exist, it will be created
50. Reading Documents
In order to read the documents which we have created
we need to use the find command
db.<Collection Name>.find()
In order to find just the documents we are looking for,
we need to add a selector
The simplest is the equality selector
.find({key: value})
51. More selectors
$ne - not equal
$gt, $gte - greater than, greater than or equal to
$lt, $lte - less than, less than or equal to
$not - logical not
52. Deleting Documents
remove - deletes documents
Without a selector it will delete all documents in a
collection - BE CAREFUL
57. mongodump
It backs up the data, it doesn’t dump it
You must have the backup role
Must have read access on the database
Super simple - mongodump
Creates a directory
58. mongorestore
restores a backed up database
Must have admin access and restore role
mongorestore —port <port no#> <path to backup>
59. mongoexport
Exports data in a MongoDB instance in either CSV or
JSON format
mongoexport —db <name> —collection <c name> —
csv
70. 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
71. Sharding
Process of splitting data up across machines
Manual sharding can be with most database
MongoDB has autosharding
Nonetheless it is difficult to configure
80. Summary
MongoDB is an open-source document database
It features JSON-style documents with dynamic
schemas
In order to gain performance, it sacrifices reliability