Channels ▼

Eric Bruno

Dr. Dobb's Bloggers

MongoDB and Java

May 22, 2011

The MongoDB project was started by 10gen about four years ago as part of a broader Platform as a Service (PaaS) offering. Around 2009, 10gen decided to focus more on MongoDB as a flagship product, given their customers' love for the product and its growing popularity. Adoption has grown consistently since then, and continues to be high with over 120,000 downloads per month, despite having a lot of competition. In fact, by 2010, MongoDB had surpassed other competitive hierarchical databases in overall usage.

In short, MongoDB is a non-relational database, where you can store data in its natural, hierarchical, form with lots of flexibility in terms of schema and queries. Labeled "NoSQL" by some, this movement has become popular, but is considered a misnomer by many people. One person who agrees is Roger Bodamer, EVP of technology at 10gen, who prefers to focus on MongoDB's ease of programming, low latency behavior, and high performance on modest hardware.

JSON-based Modeling

At the heart of MongoDB's ease of use is its JavaScript Object Notation (JSON)-based data model. Not only is JSON extremely popular in the web space — a key target market for 10gen — it's a natural way to express data models and hierarchies. The power of this approach is two-fold:

  1. It's easy to get started with MongoDB. You can quickly mock up your data model without too much concern that it will grow as you flesh out your application and its overall requirements. To extend the model, you simply provide additional JSON documents and the database aggregates them automatically. This document-oriented approach to data modeling, and MongoDB's unique use of it with what it calls collections, allows it to fit right into today's popular agile software development approach.

  2. Iterative development is a natural fit with MongoDB, as changes to the model are easily expressed and instantly reapplied through changes and additions to the JSON document model

Latency, Performance, and Scalability

As mentioned, MongoDB is designed for low latency in terms of query response. Additionally, users are continually surprised at how well MongoDB performs, and how many transactions-per-second it can achieve on modest, low-cost, hardware. Scalability is achieved by simply adding low-cost server hardware horizontally. The software handles clustering across these servers almost automatically.

To do this, MongoDB is built on what 10Gen calls automatic sharding, which balances for load and data distribution across new machines. It works across thousands of nodes, with no single point of failure, and with automatic failover across all of them. For more information on sharding, check out this link on the MongoDB website.

Simplified Query Language

A big differentiator for MongoDB among its competitors is the power and simplicity of its query language. It's an interactive query language that's simple to learn and master. The database also includes a shell that's interactive, and allows you to write, execute, and optimize queries with results returned dynamically. If you've worked with Oracle's SQL*Plus, or MySQL's Query Tool, you'll be at home with MongoDB.

One of MongoDB's best capabilities is its support for dynamic (ad hoc) queries, which is similar to how relational databases work. This makes it easy when moving from an RDBMS to MongoDB. The query language is flexible in terms of missing fields, exactly matching fields, range queries, regular expressions, and the use of JavaScript functions in queries (somewhat analogous to a stored procedure).

Most queries in MongoDB don't use JavaScript on the server (a performance and scalability concern for some), but are instead performed through the provided API. Additionally, MongoDB does support MapReduce, which is a framework that supports arbitrary and complex queries that can use JavaScript as needed.

However, server-side JavaScript execution has seen a rise in popularity recently with the growth of HTML5. As a result, JavaScript engines are being continuously enhanced and tuned in terms of performance, where some even support JIT compilation. With this associated growth and investment in JavaScript execution on the server, and its associated performance gains, 10gen isn't too worried about it. This is good news for developers of HTML5 applications, or other Ajax-based applications, where most if not all of the code is written in JavaScript.

Let's look at an example of some queries in MongoDB, and then compare it with the Java API for the same queries. Below is a sample object in MongoDB, represented as a JSON object:


{
    "first_name" : "John",
    "last_name"  : "Smith",
    "address" : {
        "street" : "123 Main Street",
        "city"   : "Anytown",
        "state"  : "NY"
   }
}

Assuming this record is added to your collection of people in the database, you can query for all records with the following:

  db.people.find( { } )

Or, you can query for all records with last name "Smith" with the following simple query:

  db.people.find( { 'last_name': 'Smith'} )

And finally, you can specifically search for John Smith with the following query:

  db.people.find( { 'last_name': 'Smith'}, {'first_name': 'John' )

There is web-specific convenience support, such as the ability to page through records to show, say, ten records at a time, skipping the previous number that have already been displayed. To do this, you use the skip and limit query keywords, as in this query:

  db.users.find().skip(20).limit(10);

This query assumes you've already shown two pages of ten results (i.e., skip(10*prev_pages_shown). MongoDB automatically handles the cursor-based query result paging for you; no application specific record keeping is needed.

The Java API

Although MongoDB is written mainly in C++, it includes APIs and drivers for most popular languages, including: C#, Java, PHP, Ruby, Python, C/C++, Haskell, Lisp, Scala, and so on. There are multiple drivers for Java support, such as a "bare bones" driver, and an evolved driver with support for object introspection. Let's look at the Java API and how it's used to query the database with queries similar to the aforementioned native ones.

Using the sample people collection from our previous example, the following Java code returns (and prints) all records in that collection:

DBCollection coll = db.getCollection("people")
DBCursor cur = coll.find();
while(cur.hasNext()) {
    System.out.println(cur.next());
}

To query for people records that match the last name of "Smith," use the following query:

DBCollection coll = db.getCollection("people")
BasicDBObject query = new BasicDBObject();
query.put("last_name", "Smith");
DBCursor cur = coll.find(query);
while(cur.hasNext()) {
    System.out.println(cur.next());
}

And finally, to specifically find John Smith, use the following query:

DBCollection coll = db.getCollection("people")
BasicDBObject query = new BasicDBObject();
query.put("first_name", "John");
query.put("last_name", "Smith");
DBCursor cur = coll.find(query);
while(cur.hasNext()) {
    System.out.println(cur.next());
}

The API is straightforward, consistent across languages, and flexible in terms of query parameters.

MongoDB Usage and 10Gen Partners

Parent company 10gen is proud to list its flagship MongoDB customers, such as Craigslist, Shutterfly, Foursquare, SourceForge, LexisNexis, the New York Times, and more. For a complete list of production deployments and customers, look here.

However, 10gen is particularly excited about its recent partnerships with RedHat and VMWare. RedHat has made MongoDB a key component of its PaaS offerings, as well as its application store. And, specifically, MongoDB has become the de facto database for VMWare's CloudFoundry offering.

With this and other new developments, MongoDB is quickly becoming the de facto database for software developers in general, across platforms. Further, 10gen is working with others, such as Microsoft for Windows Azure integration, and plan a long list of key announcements in the coming weeks.

From the developer community perspective, MongoDB is strong and growing. A complete list of MongoDB events is available online, and includes MongoDB San Francisco on May 24th, MongoDB New York on June 7th, MongoDB Washington DC on June 27th, and more. Check out the link above for full details.

Happy coding!

— EJB

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Dr. Dobb's TV