1. 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 0
Chris Edwards
2. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
3. What is the No-SQL Movement?
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 0
4. What is the No-SQL Movement?
“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
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 0
5. What is the No-SQL Movement?
“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 • Horizontally Scalable
• Distributed • Schema-Free
• Open-Source • Replication Support
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 0
6. What is the No-SQL Movement?
• Not all systems have the same data needs.
• Use the right tool for the right job.
• Sql is not always the best option, nor is it the
only one.
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 0
7. What is the No-SQL Movement?
• Not all systems have the same data needs.
• Use the right tool for the right job.
• Sql is not always the best option, nor is it the
only one.
Not Only SQL
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 0
8. What is the No-SQL Movement?
• Not all systems have the same data needs.
• Use the right tool for the right job.
• Sql is not always the best option, nor is it the
only one.
Not Only SQL
It’s not about flaming SQL.
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 0
9. What is the No-SQL Movement?
• Not all systems have the same data needs.
• Use the right tool for the right job.
• Sql is not always the best option, nor is it the
only one.
Not Only SQL
It’s not about flaming SQL.
Its about opening our minds to new technologies.
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 0
10. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
11. What is MongoDB?
• Document-oriented database
- Uses JSON (BSON actually)
• Schema-free
• Performance
- Written in C++
- Full index support
- No transactions (has atomic operations) “Mongo only pawn in game of life”
- Memory-mapped files (delayed writes)
• Scalability
- Replication
- Auto-Sharding
• Commercially supported (10gen)
- Lots of documentation
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 0
12. 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)
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 0
13. Other Features of MongoDB
• Supported Platforms
- OSX
- Linux
- Solaris
- Windows
- FreeBSD
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 0
14. MongoDB Basic Components
• The Database Server
• The Interactive Shell
• The Sharding Router
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 0
15. MongoDB Basic Components
• The Database Server
• The Interactive Shell
• The Sharding Router
mongod.exe
The database server
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 0
16. MongoDB Basic Components
• The Database Server
• The Interactive Shell
• The Sharding Router
mongod.exe mongo.exe
The database server The interactive shell
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 0
17. MongoDB Basic Components
• The Database Server
• The Interactive Shell
• The Sharding Router
mongod.exe mongo.exe
The database server The interactive shell
mongos.exe
The sharding router
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 0
18. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
19. 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
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 0
20. 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!
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 0
21. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
22. Accessing MongoDB via the Shell
• Inserting a document into a collection.
• Querying a collection.
• Modifying a document.
• Deleting a document.
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 0
23. Accessing MongoDB via the Shell
• Inserting a document into a collection.
• Querying a collection.
• Modifying a document.
• Deleting a document.
Lets do it!
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 0
24. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
25. Accessing MongoDB via C#
• mongodb-csharp driver
- Most mature driver for C#.
- Look at basic functionality.
- Then using LINQ.
• NoRM driver
- Using NoRM for strongly typed access.
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 0
26. Accessing MongoDB via C#
• mongodb-csharp driver
- Most mature driver for C#.
- Look at basic functionality.
- Then using LINQ.
• NoRM driver
- Using NoRM for strongly typed access.
For more information and/or drivers for other languages, check out www.mongodb.org
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 0
27. Accessing MongoDB via C#
Demo of using the
mongodb-csharp driver.
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 0
28. Accessing MongoDB via C#
Demo of using the NoRM driver
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 0
29. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
30. 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
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 0
31. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
32. MongoDB Replication
• Master-Slave
- Master - mongod.exe --master
- Slave - mongod.exe --source <master url>:<master port>
- Slave can use --slavedelay <delay in seconds> to have a rolling backup.
• Replica Pairs (obsolete)
• Replica Sets (to be released in 1.6)
- 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
• Master-Master (limited)
- Both masters are also configured as slaves
- Safe for insert, delete by id, and any queries
- Unsafe for concurrent updates of single object
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 0
33. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
34. MongoDB Auto-Sharding: Features
• Based on a defined shard key.
• Auto-balances as shard servers are added or
removed.
- Can go from single master to sharded system with zero downtime.
• Failover handled through replica sets. (each shard
replicated)
• Map Reduce queries are run in parallel across
shards.
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 0
35. MongoDB Auto-Sharding: Architecture
• Shard Servers
- Instance of mongod.exe --shardsvr
- Optionally configured with a replication set for failover.
• Config Servers
- Instance of mongod.exe --configsvr
- Usually a group of 3. System is up so long as 1 config server is
running.
• Shard Routers
- Instance of mongos.exe --configdb <config server>:<config port>
- Acts like mongod.exe to clients.
- Can be on the same box as a shard server.
- Can run on appserver to reduce traffic.
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 0
36. MongoDB Auto-Sharding: Architecture
Sample Logical Architecture
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 0
37. MongoDB Auto-Sharding: Architecture
Sample Physical Architecture
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 0
38. MongoDB Auto-Sharding: Configuration
Startup Servers
• Startup Shard Servers
- mongod.exe --shardsvr
• Startup Config Servers
- mongod.exe --configsvr
• Startup Shard Routers
Configure --configdb <config server>:<config port>
- mongos.exe
Cluster
• Add Shards to the Cluster
- Execute the following command for each shard using either the
driver or shell.
- db.runcommand( { addshard : “<shard server>:<shard server port>” } );
• Enable Sharding on the Databases to Shard
- db.runcommand( { enablesharding : “<dbname>” } );
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 0
39. MongoDB Session Roadmap
• What is the NoSQL Movement?
• What is MongoDB?
• Installing
• Using the Mongo Shell
• Accessing MongoDB via C#
- Basic Driver (mongodb-csharp driver)
- Using Linq
- Using NoRM for strong typing
• Accessing MongoDB via REST
• Replication
• Sharding
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 0
40. 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 0
?
41. About Me
Chris Edwards
Developer at BancVue Ltd
Email: ChrisEdwards357@gmail.com
Blog: http://
chrisedwards.dreamhosters.com
Twitter: @cedwards
Github: http://github.com/chrisedwards
BancVue
Web: www.bancvue.com
Phone: 877.342.2557
We are hiring C# developers:
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 0