Video at: http://www.chrisedwards.dreamhosters.com/blog/2011/09/10/video-introduction-to-mongodb-austin-code-camp-2011/
A high level overview of MongoDB targeting developers that have never heard of or used MongoDB. We look at its features and capabilities, how to install and configure it, and how to use it. We cover querying, indexing, sharding and replication. I try hard to state everything in terms of SQL Server to give the audience a context to help understand the new NoSql concepts in MongoDB. I also use animations to help clarify the more difficult concepts of sharding and replication.
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Introduction to MongoDB - Austin Code Camp 2011
1. +)1*3$&4(56&4#70&89::&
!"#$%&'()&*(&+,,&-).&/0($1(.12&
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
2. h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
3. Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
Chris Edwards
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
4. MongoDB
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
5. About Me
Chris Edwards
Architect at BancVue Ltd.
Email: ChrisEdwards357@gmail.com
Blog: http://chrisedwards.dreamhosters.com
Twitter: @cedwards
Github: http://github.com/chrisedwards
(Sample code for this talk is posted)
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
6. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
7. What is No-SQL?
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
8. What is No-SQL?
“Next Generation Databases mostly addressing some of the points:
being non-relational, distributed, open-source and horizontal
scalable. ... schema-free, easy replication support, simple API,
eventually consistent ...”
- nosql-database.org
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
9. What is No-SQL?
“Next Generation Databases mostly addressing some of the points:
being non-relational, distributed, open-source and horizontal
scalable. ... schema-free, easy replication support, simple API,
eventually consistent ...”
- nosql-database.org
• Non-Relational • Schema-Free
• Distributed • Replication Support
• Open-Source • Simple API
• Horizontally Scalable • Eventually Consistent
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
10. What is No-SQL?
It’s about using the right tool for the job.
• Not all systems have the same data needs.
• Sql is not the only option, nor is it always the best one.
• Consider all options carefully and choose wisely.
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
11. What is No-SQL?
It’s about using the right tool for the job.
• Not all systems have the same data needs.
• Sql is not the only option, nor is it always the best one.
• Consider all options carefully and choose wisely.
Not Only SQL
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
12. What is No-SQL?
It’s about using the right tool for the job.
• Not all systems have the same data needs.
• Sql is not the only option, nor is it always the best one.
• Consider all options carefully and choose wisely.
Not Only SQL
It’s not about flaming SQL.
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
13. What is No-SQL?
It’s about using the right tool for the job.
• Not all systems have the same data needs.
• Sql is not the only option, nor is it always the best one.
• Consider all options carefully and choose wisely.
Not Only SQL
It’s not about flaming SQL.
Its about opening our minds to new technologies.
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
14. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
15. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
16. What is MongoDB?
• Document-oriented database
- Uses JSON (BSON actually)
• Schema-free
• Performant
- Written in C++
- Full index support
- No transactions (has atomic operations)
“Mongo only pawn in game of life”
- Memory-mapped files (delayed writes)
• Replication & High Availability
• Auto Sharding
• Commercially supported (10gen)
- Lots of documentation
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
17. Other Features of MongoDB
• Document-based queries
- Flexible document queries expressed in JSON/Javascript.
• Map Reduce
- Flexible aggregation and data processing.
- Queries run in parallel on all shards.
• GridFS
- Store files of any size easily.
• Geospatial Indexing
- Find object based on location. (i.e. find closest n items to x)
• Many Production Deployments
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
18. Other Features of MongoDB
• Supported on Multiple Platforms
- OSX
- Linux
- Solaris
- Windows
- FreeBSD
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
19. MongoDB Basic Concepts
Document
- Analogous to a row in RDBMS
- Represented as JSON (BSON)
- Hierarchical
- Can reference other documents
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
20. MongoDB Basic Concepts
Collection
- Analogous to a table in RDBMS
- Collection of documents
- Documents can be anything
- Advantageous if they are similar
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
21. MongoDB Basic Concepts
Embedding { '_id': objectid('4b980a6dea2c3f4579da141e'),
- Analogous to a foreign key 'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
- Think “pre-joined relationship”.
'line_items': [
- Clearer conceptual model. { 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
- DDD Aggregate Root 'retail_price': 1099
- Can be },
- Sub Objects { 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
- Collections 'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
22. MongoDB Basic Concepts
Embedding { '_id': objectid('4b980a6dea2c3f4579da141e'),
- Analogous to a foreign key 'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
- Think “pre-joined relationship”
'line_items': [
- Clearer conceptual model. { 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
- DDD Aggregate Root 'retail_price': 1099
- Can be },
- Sub Objects { 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
- Collections 'retail_price': 1199
}
],
'shipping_address': {
References 'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
- Analogous to a foreign key 'zip': '11216'
},
- Think “relationship”
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
23. MongoDB Basic Operations
Querying
All the following queries will find the document.
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
'line_items': [
{ 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
'retail_price': 1099
},
{ 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
24. MongoDB Basic Operations
Querying
All the following queries will find the document.
By property value
db.orders.find( { "state": "in_cart" } );
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
'line_items': [
{ 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
'retail_price': 1099
},
{ 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
25. MongoDB Basic Operations
Querying
All the following queries will find the document.
By property value
db.orders.find( { "state": "in_cart" } );
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
By embedded object property value 'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
db.orders.find( { "shipping_address.state": "NY" } ); 'line_items': [
{ 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
'retail_price': 1099
},
{ 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
26. MongoDB Basic Operations
Querying
All the following queries will find the document.
By property value
db.orders.find( { "state": "in_cart" } );
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
By embedded object property value 'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
db.orders.find( { "shipping_address.state": "NY" } ); 'line_items': [
{ 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
With comparison operators 'retail_price': 1099
},
db.orders.find( { "subtotal": { $gt: 2000 } } ); { 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
27. MongoDB Basic Operations
Querying
All the following queries will find the document.
By property value
db.orders.find( { "state": "in_cart" } );
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
By embedded object property value 'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
db.orders.find( { "shipping_address.state": "NY" } ); 'line_items': [
{ 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
With comparison operators 'retail_price': 1099
},
db.orders.find( { "subtotal": { $gt: 2000 } } ); { 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
Values in collections (implicit “in”) ],
db.orders.find( { "line_items.sku": "ly‐211" } ); 'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
28. MongoDB Basic Operations
Indexing
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
'line_items': [
{ 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
'retail_price': 1099
},
{ 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
29. MongoDB Basic Operations
Indexing
On property value
db.orders.ensureIndex( { "state": 1 } );
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
'line_items': [
{ 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
'retail_price': 1099
},
{ 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
30. MongoDB Basic Operations
Indexing
On property value
db.orders.ensureIndex( { "state": 1 } );
On embedded object property
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
db.orders.ensureIndex( { "shipping_address.state": 1 } ); 'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
'line_items': [
{ 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
'retail_price': 1099
},
{ 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
31. MongoDB Basic Operations
Indexing
On property value
db.orders.ensureIndex( { "state": 1 } );
On embedded object property
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
db.orders.ensureIndex( { "shipping_address.state": 1 } ); 'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
'line_items': [
On embedded collections { 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
db.orders.ensureIndex( { "line_items.sku": 1 } ); 'retail_price': 1099
},
{ 'sku': 'ly‐211',
'name': 'Larry Young: Unity',
'retail_price': 1199
}
],
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
32. MongoDB Basic Operations
Indexing
On property value
db.orders.ensureIndex( { "state": 1 } );
On embedded object property
{ '_id': objectid('4b980a6dea2c3f4579da141e'),
db.orders.ensureIndex( { "shipping_address.state": 1 } ); 'user_id': objectid('4b980a6dea2c3f4579a4f54'),
'state': 'in_cart',
'line_items': [
On embedded collections { 'sku': 'jc‐432',
'name': 'John Coltrane: A Love Supreme',
db.orders.ensureIndex( { "line_items.sku": 1 } ); 'retail_price': 1099
},
{ 'sku': 'ly‐211',
On multiple keys 'name': 'Larry Young: Unity',
'retail_price': 1199
db.orders.ensureIndex( { "shipping_address.state": 1, }
],
"subtotal": ‐1 } );
'shipping_address': {
'street': '3333 Greene Ave.',
'city': 'Brooklyn',
'state': 'NY',
'zip': '11216'
},
'subtotal': 2199
}
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
33. MongoDB Basic Operations
Inserting
db.orders.save( {
'user_name': 'Bob Smith',
'state': 'in_cart',
'shipping_address': {
'street': '1234 Morgan Ave.',
'city': 'Austin',
'state': 'TX',
'zip': '78660'
},
'subtotal': 0
} );
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
34. MongoDB Basic Operations
Updating
Using Query/Modify/Save
var order = db.orders.find( { 'user_name': 'Bob Smith' } );
order.state = 'in_cart';
order.line_items.push( { 'sku': 'md‐12',
'price': 2500,
'title': 'Basketball' } );
order.subtotal = order.subtotal + 2500;
db.orders.save( order );
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
35. MongoDB Basic Operations
Updating
Using Query/Modify/Save
var order = db.orders.find( { 'user_name': 'Bob Smith' } );
order.state = 'in_cart';
order.line_items.push( { 'sku': 'md‐12',
'price': 2500,
'title': 'Basketball' } );
order.subtotal = order.subtotal + 2500;
db.orders.save( order );
• Requires each object to be loaded in memory.
• Requires multiple hits to the database.
• Very inefficient.
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
36. MongoDB Basic Operations
Updating
Using Atomic Updates
db.orders.update( { '_id': order_id },
{ $set: { 'state': 'in_cart' },
$push: { 'line_items': { 'sku': 'md‐12',
'price': 2500,
'title': 'Basketball' }
},
$inc: { 'subtotal': 2500 }
} );
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
37. MongoDB Basic Operations
Updating
Using Atomic Updates
db.orders.update( { '_id': order_id },
{ $set: { 'state': 'in_cart' },
$push: { 'line_items': { 'sku': 'md‐12',
'price': 2500,
'title': 'Basketball' }
},
$inc: { 'subtotal': 2500 }
} );
• Analogous to transactions.
• Requires only one trip to the database.
• Operations are atomic.
• Can perform multiple operations at once.
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
38. MongoDB System Components
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
39. MongoDB System Components
mongod.exe
The database server
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
40. MongoDB System Components
mongod.exe mongo.exe
The database server The interactive shell
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
41. MongoDB System Components
mongod.exe mongo.exe
The database server The interactive shell
mongos.exe
The sharding router
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
42. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
43. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
44. Installing MongoDB
1. Download MongoDB. www.mongodb.com/downloads
2. Extract it.
3. Create the data folder. usually /data/db -or- C:datadb
4. Run mongod.exe
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
45. Installing MongoDB
1. Download MongoDB. www.mongodb.com/downloads
2. Extract it.
3. Create the data folder. usually /data/db -or- C:datadb
4. Run mongod.exe
That’s it!
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
46. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
47. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
48. Accessing MongoDB via the Shell
• Inserting a document into a collection.
• Querying a collection.
• Modifying a document.
• Deleting a document.
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
49. Accessing MongoDB via the Shell
• Inserting a document into a collection.
• Querying a collection.
• Modifying a document.
• Deleting a document.
Lets do it!
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
50. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
51. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
52. Accessing MongoDB via REST
• To enable basic REST interface, use --rest command line.
- mongod.exe --rest
• REST interface uses port +1000.
- http://127.0.0.1:28017/database/collection/
- http://127.0.0.1:28017/database/collection/?filter&Field=Value
• Full REST support is provided by the Sleepy.Mongoose lib.
- http://github.com/kchodorow/sleepy.mongoose
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
53. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
54. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via REST
• Replication
• Sharding
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
55. MongoDB Replication
Replica Sets
- Full Failover support
- Supports more than 2 servers in a replica cluster
- Data center and rack aware
- Can have passive set members (slaves) that are never primary
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
56. MongoDB Replication
Node A
Primary
Node B Node C
Secondary Secondary
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
57. MongoDB Replication
1∆ Node A
Primary
Node B Node C
Secondary Secondary
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
58. MongoDB Replication
Node A
Primary
1∆
Node B Node C
Secondary Secondary
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
59. MongoDB Replication
Node A
Primary
1∆
Node B Node C
Secondary Secondary
1∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
60. MongoDB Replication
Node A
Primary
1∆
Node B Node C
Secondary Secondary
1∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
61. MongoDB Replication
2∆ Node A
Primary
1∆
Node B Node C
Secondary Secondary
1∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
62. MongoDB Replication
Node A
Primary
1∆ 2∆
Node B Node C
Secondary Secondary
1∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
63. MongoDB Replication
Node A
Primary
1∆ 2∆
Node B Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
64. MongoDB Replication
Node A
Primary
1∆ 2∆
Node B Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
65. MongoDB Replication
3∆ Node A
Primary
1∆ 2∆
Node B Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
66. MongoDB Replication
Node A
Primary
1∆ 2∆ 3∆
Node B Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
67. MongoDB Replication
NodeiA e
n
O ffl Primary
1∆ 2∆ 3∆
Node B Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
68. MongoDB Replication
NodeiA e
n
O ffl Primary
1∆ 2∆ 3∆
Node B Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
69. MongoDB Replication
READ
NodeiA e
n
ONLY
O ffl Primary
1∆ 2∆ 3∆
Node B Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
70. MongoDB Replication
READ
NodeiA e
n
ONLY
O ffl Primary
1∆ 2∆ 3∆
Node B FIGHT! Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
71. MongoDB Replication
READ
NodeiA e
n
ONLY
O ffl Primary
1∆ 2∆ 3∆
Node B Vote Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
72. MongoDB Replication
READ
NodeiA e
n
ONLY
O ffl Primary
1∆ 2∆ 3∆
Node B ME! Node B
Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
73. MongoDB Replication
READ
NodeiA e
n
ONLY
O ffl Primary
1∆ 2∆ 3∆
Node B Node C
Secondary Secondary
1∆ 2∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
74. MongoDB Replication
READ
ONLY
Node B
Secondary
1∆ 2∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
75. MongoDB Replication
READ
ONLY
Node B
Secondary
1∆ 2∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
76. MongoDB Replication
Node B
Primary
1∆ 2∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
77. MongoDB Replication
Node B
Primary
1∆ 2∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆ 2∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
78. MongoDB Replication
Node B
Primary
1∆ 2∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆ 2∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
79. MongoDB Replication
4∆ Node B
Primary
1∆ 2∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆ 2∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
80. MongoDB Replication
Node B
Primary
1∆ 2∆ 4∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆ 2∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
81. MongoDB Replication
Node B
Primary
1∆ 2∆ 4∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆ 2∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
82. MongoDB Replication
Node B
Primary
1∆ 2∆ 4∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆ 2∆ 4∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
83. MongoDB Replication
Node B
Primary
1∆ 2∆ 4∆
NodeiA e
n Node C
O ffl Primary Secondary
1∆ 2∆ 3∆ 1∆ 2∆ 4∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
84. MongoDB Replication
Node B
Primary
1∆ 2∆ 4∆
Node A Node C
Primary Secondary
1∆ 2∆ 3∆ 1∆ 2∆ 4∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
85. MongoDB Replication
Node B
Primary
1∆ 2∆ 4∆
Node A Node C
Recovering... Secondary
1∆ 2∆ 3∆ 1∆ 2∆ 4∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
86. MongoDB Replication
Node B
Primary
1∆ 2∆ 4∆
Node A Node C
Recovering... Secondary
1∆ 2∆ 3∆ 1∆ 2∆ 4∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
87. MongoDB Replication
Node B
Primary
1∆ 2∆ 4∆
Node A Node C
Recovering... Secondary
1∆ 2∆ 3∆ 1∆ 2∆ 4∆
h t t p : / / c h r i s e d wa r d s . d r e a m h o s t e r s . c o m
Introduction to MongoDB
A No-SQL Persistence Alternative
A U S T I N - C O D E - C A M P - 2 0 1 1
Notas del editor
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
This is a test note\n
\n
Collection\n - Documents can be anything\n - Advantageous if they are similar\n