Chapter 11. Example Applications

Throughout this text, almost all of the examples have been in JavaScript. This chapter explores using MongoDB with languages that are more likely to be used in a real application.

Chemical Search Engine: Java

The Java driver is the oldest MongoDB driver. It has been used in production for years and is stable and a popular choice for enterprise developers.

We’ll be using the Java driver to build a search engine for chemical compounds, heavily inspired by This search engine has the chemical and physical properties of thousands of compounds on file, and its goal is to make this information fully searchable.

Installing the Java Driver

The Java driver comes as a JAR file that can be downloaded from Github. To install, add the JAR to your classpath.

All of the Java classes you will probably need to use in a normal application are in the com.mongodb and com.mongodb.gridfs packages. There are a number of other packages included in the .JAR that are useful if you are planning on manipulating the driver’s internals or expanding its functionality, but most applications can ignore them.

Using the Java Driver

Like most things in Java, the API is a bit verbose (especially compared to the other languages’ APIs). However, all of the concepts are similar to using the shell, and almost all of the method names are identical.

The com.mongodb.Mongo class creates a connection to a MongoDB server. You can access a database from the connection and then get ...

Get MongoDB: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.