- Overview
-
Table of Contents
- J2SE: Standard Java
- Java Windows NT Services
- Apache Velocity
- Advanced J2SE
- Bytecode Instrumentation
- Dynamic Languages and the JVM
- J2SE 1.5.0: "Tiger"
- Java SE 6
- Java 7
- Core Computer Science Principles in Java (Data Structures)
- Annotations
- Java Generics
- Java New I/O
- Java Sound
- Java Applets
- JavaFX
- Java SE Threading
- Resource Management Using Semaphores
- Java Atomic Operations
- JavaTemplate Pages
- Executing Templates with the JtpExecutor
- Java Cryptography Extensions (JCE)
- Java Database Connectivity (JDBC) API
- Jakarta Commons - Net Class Library
- Jakarta Commons HttpClient
- Apache POI
- Regular Expressions
- JavaMail
- Cool Tools
- Building an Really Simple Syndication (RSS) Java App
- Embedding JavaScript in Java with Rhino
- Logging with Log4J
- Inside Swing
- Swing Components
- SwingX
- Swing Styled Documents
- Web Rendering in Java Swing Applications
- Java Look-and-Feel Graphics Repository
- Java Media Framework
- Quicktime for Java
- Media in Java Review 2008
- External Multimedia in Java
- Graphs and Charts
- Holiday Special: Electronic Greeting Card
- Media Framework: Presenter Application
- Standard Widget Toolkit
- JFace
- Java Performance Tuning
- J2EE Performance Tuning
- Caches and Pools
- Java Caching System
- EHCache
- Java Compression and Decompression
- Obfuscating Java Applications
- Continuous Integration
- Load Testing
- Tomcat Clustering
- High Scalability with Terracotta
- Troubleshooting Production Performance Issues
- Enterprise Java Testing
- Automated Unit Testing with JUnit and Ant
- Unit Testing: Tips From The Trenches
- Custom Ant Tasks
- Extensible Markup Language (XML)
- Java Web Technologies
- Web Frameworks
- Struts 2
- Wicket
- JavaServer Faces
- Distributed Programming / RMI
- Behavior Tracking Servlet Filter
- Servlet Filters
- Building a Robust Java Server
- J2EE: Enterprise Java
- Spring
- Spring 3
- Java Design Patterns
- Model-Driven Architecture
- Enterprise Messaging with ActiveMQ
- Event-Driven Architecture
- XDoclet
- Hibernate
- Developing Standalone Database Applications with Hypersonic DB
- Project Backup
- J2EE Project: Hands-On
- Enterprise Java Beans (EJB) 3.0
- Disaster Recovery
- Java Management Extensions (JMX)
- Service-Oriented Architecture
- Web Services
- RESTful Web Services
- Web Services with Apache CXF
- Atom Syndication
- Project: Building a Web Photo Gallery
- J2ME: Micro Java
- Specialized J2ME
- Optional Packages
- Other Java Technologies
- Derivatives and Competitors
- Java, Engineered for Integration
-
Additional Resources
- The World of Java Tools
- Building Java Applications with Ant
- Managing Java Build Lifecycles with Maven
- Acceptance Testing with FitNesse
- Source Control with Subversion
- Inversion of Control and Dependency Injection
- Certification
- Roadmap: Becoming an Enterprise Java Developer
- Roadmap: Becoming an Enterprise Java Developer in 2007
- The Business of Enterprise Software
- JavaOne 2006
- JavaOne 2007
- JavaOne 2008 Wrap-Up
- JavaOne 2009 Wrap-Up
- JavaOne 2010
- JavaOne 2011
- How to Survive in a Turbulent Job Market
- How to Hire the Best Talent
- Unified Modeling Language (UML)
- Cloud Computing
- Amazon EC2 and Java
- MongoDB
- Enterprise Java in 2008 and Beyond
- Predictions for 2018
MongoDB
Last updated Mar 14, 2003.
As modern software developers we are intimately familiar with relational databases. We understand how to elegantly normalize a database, and if we've been working with database for some time, we understand what things we can do to de-normalize a database to enhance performance. We are familiar with SQL and with joining different tables to obtain the information we seek. We are also familiar with some of the programmatic discrepancies between relational data and object-oriented programming and hence the creation of Object-Relational Mapping (ORM) tools like Hibernate that bridge those discrepancies. Relational databases are the underpinnings that sit below most of our enterprise applications.
If relational databases are so fundamental to our applications, then where did "NoSQL" databases such as MongoDB come from? The simple answer is that relational models and relational database transactional overhead are an impediment to scalability. Given, relational databases can scale quite large to host terabytes of information, but this is not the level of scalability to which I am referring. Consider storing user feeds on a social networking site like Facebook or Twitter. In this case we're not talking about terabytes of data, but rather pentabytes of data - and growing! In such an environment, a relational database simply cannot store and provide access to this much information in an acceptable manner.
There are certain types of database interactions that necessitate transactional integrity, such as credit card payments, but for the vast majority of database interactions in large scale web applications, such as social network or even product catalogs, transactional integrity is not a strict requirement. If you could remove transactions and de-normalize such a model then you could distribute data across hundreds, or even thousands, of machines and intelligently find that data when it is requested.
MongoDB is a document database designed from its inception to support modern web applications and horizontal-scalability, meaning that it can easily be distributed across dozens of machines as application needs grow. But document databases represent a change in mindset from an existing relational representation of data into document-oriented representation of data. Let's consider the user feeds referenced above. Listing 1 shows a document that might represent a feed posting.
Listing 1. Feed Posting Document
{ _id: ObjectID('12345...'),
message: 'Feeling good today',
user: 'shaines',
picture: {
url: 'http://media.geekcap.com/pictures/pic.jpg',
title: 'Beautiful Sunrise'
},
comments: [
{user: 'michael',
message: 'Good to hear'},
{user: 'rebecca',
message: 'That makes me happy'}
]
}
Listing 1 looks far more natural than you were probably conceiving of a post in a relational manner. The document contains the user that posted the message, the message itself, a picture associated with the post, and a list of comments. The document is represented in JSON, which is one of the formats that MongoDB supports, and is very readable.
To contrast this against a relational model, Figure 1 shows a domain model that might represent this same information in a relational way.
Figure 1 Feed Relational Domain Model
In order to remove the chance of duplicating data, the domain model presented in figure 1 is normalized. It is a clean relational representation of the data, but as a result we need several joins or even separate queries (for comments) in order to build a complete feed to display to the user. The document, on the other hand, includes all data in one single record. For example, to query for all feeds we would perform a query similar to the following:
SELECT * FROM feed INNER JOIN user ON feed.user_id = user.id INNER JOIN picture ON feed.id = picture.feed_id WHERE user.username = 'shaines';
And then to get comments you would need to iterate over the feeds and load the comments:
SELECT * FROM comment WHERE feed_id = ?
Now let's look at using MongoDB's query language to find feeds written by me:
db.feed.find( {'user': 'shaines'} );
This statement would return all documents where the user property is 'shaines'. It is a different way of thinking of things, but you have to admit that it is pretty clean.
This series will demonstrate how to setup and configure MongoDB, how to interact with it in Java, and some of the libraries that help you more easily interact with MongoDB.
Continue to MongoDB Setup.
