NoSQL in Practice with a CRUD Application in Java

Creating a CRUD application in Java and MongoDB is pretty simple to begin with, although there are many intricacies involved as we dive deeper. The architectural foundation of the MongoDB JDBC library (version 3) quite aligns with the need of the Java developer. The API is intuitive and performs well with document schema that are translated in BSON. The JDBC driver version 3 has substantial changes in the API than its older (version 2) counterpart. So, the code written for one version can be significantly different than the other. Here, we mainly follow version 3 (the latest is v3.2.1 to be specific as of writing this article) APIs while creating a CRUD application in Java. The article gives you a glance at the practical aspects of NoSQL when implemented in MongoDB and Java.

A Glimpse into MongoDB

MongoDB is a document-oriented NoSQL database. If you are familiar with any relational databases, such as an RDBMS package, what a record is to relational database, a document is to MongoDB. Similarly, what a table is to relational database, a collection is to MongoDB. A data item in a document is structured as a key-value pairs much like a JSON object. The value can be of varied type. It may be a string, numbers, arrays, other document, or an array of documents. Thus, a typical document and a collection may be as follows:

Figure 1: The document/collection connection

CRUD in a Mongo Shell

Before going into Java code, let's first walk through CRUD operations in the mongo shell. Once MongoDB has been installed properly (see installation instructions), it provides an interactive shell called mongo. This shell can be invoked when required to directly interact with the database. The core database process, a daemon called mongod, must be up and running by invoking the mongo console. It can be executed as follows (unless mongod is already running):

Update Document

Delete Document

Given, the preceding are very rudimentary CRUD operations executed from a mongo shell. Now, let's see how we can achieve the same CRUD effect through Java code.

CRUD Application in Java

It is pretty simple to code a rudimentary CRUD implementation in Java. Before delving into the code, download the JDBC driver and include the JAR file in the project build path, or follow the installation guide.

Observe that the code, in essence, is somewhat similar to database access in a relational model, although the underlying principle is very different.

Conclusion

The article tried to give you a hands-on example of how to leverage Java and MongoDB with the help of a CRUD example without delving into much conceptual detail. The JDBC API library is highly intuitive and quite straightforward in dealing with connectivity as well as data manipulation through API functions. The intuitive appeal does not get diminished at all even as we go deeper into the complexities of a CRUD application. We shall see some aspects of it in the next few articles.

IT Solutions Builder
TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD

Which topic are you interested in?

Mobile

Security

Networks/IoT

Cloud

Data Storage

Applications

Development

IT Management

Other

What is your company size?

What is your job title?

What is your job function?

Searching our resource database to find your matches...

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.

Thanks for your registration, follow us on our social networks to keep up-to-date