VOOZH about

URL: https://www.geeksforgeeks.org/java/mongodb-tutorial-in-java/

⇱ MongoDB Tutorial in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

MongoDB Tutorial in Java

Last Updated : 12 Jul, 2025

MongoDB is an open-source cross-platform document database developed using C++. Some features of MongoDB are:

  • High and effective performance
  • Easily scalable
  • High availability
  • It can store high volume of data

It contains data in the form of collections and documents instead of rows and tables. A collection is a set of documents. The collection does not have schemas. It represents data in the form of a hierarchical model with which the storage of arrays and other data structures will be easy.

Components of MongoDB

The essentials components of MongoDB are listed below:

  1. id: This field represents a unique field in MongoDB. This field is created by default.
  2. Collection: It is a set of MongoDB documents. It exists with a single database.
  3. Database: This is the container for collections. Multiple databases can be stored in a mongoDB server.
  4. Document: A record in mongoDB is called a document. It contains names and values.
  5. Field: It is a name-value pair in a document.

Note: Make sure to install and setup MongoDB JDBC driver and Java.

Table of contents:
1. Establishing connections to database
2. Creating a MongoDb collection
3. Getting a Collection
4. Inserting Values into MongoDb
5. Displaying the list of all Documents
6. Updating documents in the MongoDB
7. Deleting a Document
8. Dropping of a Collection
9. Displaying all the collections

Establishing connections to database

For making the connection, you have to mention the database name. MongoDB creates a database by default if no name is mentioned.

  1. Firstly, import the required libraries for establishing the connection.
  2. Here, "MongoClient" is used to create the client for the database.
  3. "MongoCredential" is used for creating the credentials.
  4. And finally, to access the database "MongoDatabase" is used.
  5. Username will be: "GFGUser" and the database name will be "mongoDb".
  6. The function ".toCharArray()" is used to convert the password into a character array.
  7. The function ".getDatabase()" is used for getting the database.

The following code establishes a connection to MongoDB -> 

Output: 👁 Image

Creating a MongoDb collection:

To create a collection com.mongodb.client.MongoDatabase class and createCollection() method is used. Here, "database.createCollection()" creates a collection named as "GFGCollection". Following is the code for creating collection: 

Output: 👁 Image

Getting a Collection

For getting a collection, MongoCollection.getCollection() method is used. Below is the implementation of this approach: 

Output 👁 Image

Inserting Values into MongoDb

Only a document type of data can be inserted a MongoDB. Therefore, either we can create a document with the values to be inserted using append() method or pass a document directly into the MongoDB using .insert() method. Here, first, we created a new document as "title" and then append the "about" section. Then, we have given the respective values to the documents. The function ".insertOne()" is used to insert the document into the collection. Below is the implementation of this approach: 

Output: 👁 Image

Displaying the list of all Documents

For displaying all documents of collection, find() method is used. Here, the database has two documents namely "document" and "document1", which are retrieved using find() method. We use an iterator since it will iterate over each document present in the list and display it to us. Following is code for displaying all the documents: 

Output: 👁 Image

Updating documents in the MongoDb

For updating the document, updateOne() method is used. Here, "Filters.eq" creates a filter that matches all documents with the name provided as argument. "Updates.set()" is used to update the document as the given value in the argument. Following is the code for it: 

Output: 👁 Image

Deleting a Document

For deleting the document, deleteOne() method is used. Following is the code for deleting the document -> 

Output: 👁 Image

Dropping of a Collection

"Collection.drop()" is used to drop the created collection. Following is the code for dropping the collection: 

Output: 👁 Image

Displaying all the collections

For displaying the list of all collections, listCollectionNames() method is used. Here, we iterate over all the collections we created with the help of "for()" statement. Database.listCollectionNames() is used to display the list of all collections present in the database. Following is the code for displaying all the collections: 

Output: 👁 Image

Comment
Article Tags:
Article Tags: