Performing CRUD operation on MongoDB using Java


In this post we will create a Java application which will interact with MongoDB to perform all the CRUD operations. 

Before going to MongoDB, lets understand 

What is NoSQL database.

To start with, these database are non-relational database, but that doesn't mean that the NoSQL database cannot maintain relationship. These database are different from relational database in the way the data is stored. NoSQL data model keeps the related data nested to single data structure.
It can be called as "non SQL" or "not only SQL" database.

What is MongoDB ?

As stated on MongoDB official website: 
"MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need"
MongoDB is a NoSQL database which stores data in Json like documents, means that the number/type of fields can vary from document to document. It is a distributed database at its core, so high availability, horizontal scaling and easy to use.

And on top of it, its free to use. So lets use it to create, update, retrieve and delete data from MongoDB using Java.

Like any other database, MongoDB provide drivers to connect. That can be added as dependency in our pom file.
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.1</version>
</dependency>

Basics of MongoDB

Database

In MongoDB, database is collection of documents. A single MongoDB server can have multiple databases.

You can create database using below command. It will access the database if it exists, else it will create the database when you store data to it. The command "dbs" will let you know which db you are already accessing.
> use mydb
switched to db mydb
> dbs
mydb

Collections

In MongoDB, Documents can be compared to tables in relational database. Only change here is that these documents are non tabular. The below command can be used to create collection.

db.createCollection(<collectionName>)
To list down all the collections in the database, use below command

db.getCollectionNames()

Documents

In MongoDB, documents can be compared to rows in relational database. Only change here is that these documents are JSON like records. 

{ "_id" : { "$oid" : "5ede054470d5755464d4f37a" }, "name" : "NewDoc", "type" : "database", "count" : 5, "versions" : ["v3.2", "v3.0", "v2.6"], "info" : { "x" : 203, "y" : 102 } }

Code-Snippet

Creating MongoDB Client

Lets try using Java to create the MongoDB client which will be used to connect the database. You can use either MongoClient or MongoClientURI to create MongoClient object.

    private static MongoClient getMongoClient(){
// This will create client for default host localhost and default port 27017
// MongoClient client = new MongoClient();
// This will create client for given host and default port 27017
// MongoClient client = new MongoClient("localhost");
// This will create client for given host and port
// MongoClient client = new MongoClient("localhost", 27017);
// We can use the connection string to get the client
MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
MongoClient mongoClient = new MongoClient(connectionString);

return mongoClient;
}

List all Database names

Using the MongoClient object created above, we can list down all the databases created using listDatabaseNames() method.

//Listing all the database
MongoIterable<String> itr = client.listDatabaseNames();
for (MongoCursor<String> it = itr.iterator(); it.hasNext(); ) {
System.out.println(it.next());
}

Accessing given Database

Using the MongoClient object, we can get specific database using getDatabase() method.

//Access given database
MongoDatabase db = client.getDatabase("mydb");
System.out.println(db.getName());

Create and Access Collection

Using the MongoClient object, we can create a new collection and access the new collection using getCollection() method.

//Create a collection under mydb
db.createCollection("mycol");

//Access given collection
MongoCollection<Document> collection = db.getCollection("mycol");

Create Documents

We have to create Document object to store each record. And use the collection created above to insert these documents to specific collection.

Document document = new Document("name", "NewDoc")
.append("type", "database")
.append("count", 1)
.append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
.append("info", new Document("x", 203).append("y",102));
collection.insertOne(document);
System.out.println("After adding one document to the collection, total count of documents is "+collection.count());
After adding the document to the collection, we can get the count of documents present in the collections using collections.count() method. 

We can insert either one document or list of documents to a collection.

MongoCollection<Document> collection = db.getCollection("mycol");
List<Document> documents = new ArrayList<>();
Document document1 = new Document("name", "SecDoc")
.append("type", "middleware")
.append("count", 2)
.append("versions", Arrays.asList("v1.2", "v1.0"));
Document document2 = new Document("name", "ThirdDoc")
.append("type", "database")
.append("count", 3)
.append("versions", Arrays.asList("11g", "12c", "18c", "19c"));

documents.add(document1); documents.add(document2);
collection.insertMany(documents);
Now if you execute the collections.count() method, you will get count as 2.

List all Documents

Like listing all the databases, you can list down all the Documents using the find() method which provides the iterator on the MongoCursor and even print it in JSON format using toJSON() method.

public static void listAllDocuments(MongoCollection<Document> collection){
System.out.println("Listing all documents in the collection in JSON format");
MongoCursor<Document> cursor = collection.find().iterator();
while(cursor.hasNext()){
System.out.println(cursor.next().toJson());
}
}
We can also use the collection.find().first() method to get the first document/record from the collection.

Retrieving Documents

In order to fetch some documents on desired constraints, we can use the find() method with the Filters class which contains all the utilities that can be used in the finding the documents. This is analogous to the where clause in relational database.
We have to statically import Filters class

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;
And the same can be used in the find() method to apply the where clause.

public static void queryDocuments(MongoClient client){
MongoDatabase db = client.getDatabase("mydb");
MongoCollection<Document> collection = db.getCollection("mycol");
Document docTypeMiddleware = collection.find(eq("type","middleware")).first();
System.out.println("Document where type=middleware: ");
System.out.println(docTypeMiddleware.toJson());

Document docTypeDBCount1 = collection.find(and(eq("type", "database"), eq("count", 1))).first();
System.out.println("Document where type=database and count=1");
System.out.println(docTypeDBCount1.toJson());
}

Update Documents

We can update the documents created using updateMany() or updateOne() methods. It returns UpdateResult class with which we can get the count of records updated.

public static void updateDocuments(MongoClient client){
MongoDatabase db = client.getDatabase("mydb");
MongoCollection<Document> collection = db.getCollection("mycol");
UpdateResult updateResult = collection.updateMany(eq("type","database"), new Document("$set", new Document("count", 5)));
System.out.println(updateResult.getModifiedCount());
RestAPI.listAllDocuments(collection);

Delete Documents

We can also delete documents which is comparable to deleting rows from tables in relational database. It return DeleteResult class, using which we can get the number of records deleted.

public static void deleteDocument(MongoClient client){
MongoDatabase db = client.getDatabase("mydb");
MongoCollection<Document> collection = db.getCollection("mycol");
DeleteResult deleteResult = collection.deleteOne(eq("type", "middleware"));
System.out.println(deleteResult.getDeletedCount());

RestAPI.listAllDocuments(collection);
}

This concludes on creating, retrieving, updating and deleting a document in MongoDB. These are some basic operation to start using a NoSQL database. It has a lot of other thing, but this should be a good starting point to use it your projects for basic operations.

You can find the entire code base here. Github Link

Reference


Comments