Posts

On MongoDB 3.0 and Java

MongoDB 3.0 marks a major new release for MongoDB and is chock-full of new features. Of those, two really stand out and require a bit of attention from Java developers:

  1. The new WiredTiger storage engine, and
  2. The new challenge-response mechanism SCRAM-SHA-1 for authenticating users with passwords that replaces the old MONGODB-CR method used prior to MongoDB 3.0

So, as a Java developer, what do you need to do to make your application compatible with MongoDB 3.0?

1. Upgrade to MongoDB Java driver 2.13

It is essential that you upgrade your MongoDB driver to the latest version 2.13. It turns out that with the new storage engine WiredTiger (and other future storage engines) queries for which collections exist in a database need to use the new listCollections command. The old way via system.namespaces will only work for the default MMaP storage engine. Hence, if you use an older version of the Java driver and you call your
db.getCollectionNames();
you won’t actually see any collections in case you’re using WiredTiger.
The new 2.13 driver actually contains two code paths – one that calls listCollections for MongoDB 3.0+ instances, and one that goes via system.namespaces for older versions of MongoDB. So updating your driver is really all you need to do to support WiredTiger databases.

2. Replace MongoCredential#createMongoCRCredential calls with MongoCredential#createCredential calls

If your application makes use of MongoDB’s user authentication (which it really should), you need to change the way you create your credentials. As the old MONGODB-CR challenge-response mechanism has been replaced with SCRAM-SHA-1, you need to replace all your calls to MongoCredential#createMongoCRCredential with calls to MongoCredential#createCredential. So, what used to be something like this

MongoCredential cred = MongoCredential.createMongoCRCredential(userName, db, pwd);

now needs to be

MongoCredential cred = MongoCredential.createCredential(userName, db, pwd);

That’s it! Your Java app should be ready to play ball with MongoDB 3.0 :-)