MongoDB With Java Driver

MongoDB With Java Driver

Introduction

NoSQL, or ‘Not Only SQL’, represents the new class of data management designed to meet the increasing volume, velocity, and variety of data that organizations are storing, processing, and analyzing. Compared to relational databases, NoSQL databases are more scalable and provide superior performance.

When considering or evaluating NoSQL solutions, the range of options for selecting a database seem to be overwhelming. NoSQL Databases like Cassandra, HBase, MongoDB, Neo4j, Redis etc, can be distinguished from one another based on their storage and access methodologies. Hence, we need to settle down for a database that is the suitable for our application as well as our business needs.

MongoDB

MongoDB is an open-source, leading NoSQL Document store database. MongoDB stores all documents as (keys with values) JSON-like documents with dynamic schemas. MongoDB calls the format BSON (Binary JSON). MongoDB provides high performance, high availability, and easy scalability.

In this blog post, we have provided an explanation about the basic features of MongoDB, MongoDB terms, and performing DML operations using Java.

Use Case

This use case deals with performing DML operations in MongoDB, using Mongo Java driver in Java.

What we need to do:

  • Prerequisites
  • Understand the terms, document structure and data modeling patterns in MongoDB
  • Create a Java Program to perform DML operations in MongoDB

Solution

Prerequisites

Data Modeling Terms and Patterns

In MongoDB, we must determine and declare a table’s schema before inserting data. The following table shows the terms used in RDBMS and the corresponding MongoDB terms

RDBMS

MongoDB

Database

Database

Tables

Collection

Rows

Documents

Column

Fields

Index

Index

Table joins

Embedded documents

Primary Key

Primary Key default(_id)

groupBy

Aggregation pipeline

The key decision in designing data models for MongoDB applications revolves around the structure of documents and how the application represents relationships between data. There are two tools that allow applications to represent these relationships: References and Embedded documents.

  • References (Normalized Data Model)

References store the relationships between data by including links or references from one document to another.

normalized_data_model

  • Embedded (De-normalized data model)

Embedded documents capture relationships between data by storing related data in a single document structure. MongoDB documents make it possible to embed document structures as sub-documents in a field or array within a document.

denormalized_data_model

Data Models patterns between documents

  • One-to-One relationship with embedded documents

This document describes a data model that uses Embedded (De-normalized) documents to describe relationships between connected data.

Pattern:
Consider the following example that maps patron and address relationships. In this One-to-One relationship between patron and address data, the address belongs to the patron.

In the normalized data model, the address document contains a reference to the patron document.

If the address data is frequently retrieved along with the name information, then with referencing, our application would have to issue multiple queries to resolve the reference. The better data model would be to embed the address data in the patron data, as in the following document:

With the embedded data model, our application would be able to retrieve the complete patron information with a single query.

  • One-to-Many relationship with Embedded Documents

This document describes a data model that uses embedded (De-normalized) documents to describe relationships between connected data.

Pattern:

Consider the following example that maps patron and multiple address relationships. In this One-to-Many relationship between patron and address data, the patron has multiple address entities.

In the normalized data model, the address documents contain a reference to the patron document.

To avoid multiple queries to resolve the references, a more optimal schema would be to embed the address data entities in the patron data, as in the following document:

With the embedded data model, our application can retrieve the complete patron information with a single query.

  • One-to-Many relationship with Document References

This document describes a data model that uses references (Normalized) between documents to describe relationships between connected data.

Pattern:

Consider the following example that maps publisher and book relationships. Embedding the publisher document inside the book document would lead to repetition of the publisher data, as shown in the following documents:

To avoid repetition of the publisher data, use references and keep the publisher information in a separate collection from the book collection and keep the publisher data reference inside the book collection.

Java Program to perform DML operations

Here’s the Java code below to create a database, to which we add a collection, and then add documents to that collection. After which, we perform DML operations on the documents. Read the comments section to get an idea about what each section of the code does.

The Source file is a comma separated file(.csv) that can be downloaded from the below location:

https://data.sfgov.org/api/views/wc6f-brai/rows.csv?accessType=DOWNLOAD

Conclusion 

We have successfully been able to interact and perform DML operations with MongoDB using Mongo-java-driver in Java. MongoDB is built for scalability, performance and high availability from single server deployments to large, complex multi-site architectures. MongoDB provides high performance for both read and write operations.

Reference

4408 Views 4 Views Today