Open In App

How to Use MongoDB in Eclipse?

Improve
Improve
Like Article
Like
Save
Share
Report

Eclipse is an IDE (integrated development environment) used in computer programming. Eclipse is an IDE in which we can use any type of programming language for which plugins are available. It contains a base workspace and an extensible plug-in system for customizing the environment. Eclipse is free and open-source software. It is written in Java and is considered to be the most popular Java IDE. It works on all main platforms, including Linux, Windows, Mac OS, etc., and has many powerful features that can be used to work out in many projects. It also provides documentation and modeling support and offers UML, OCL, SysML, and implementation tools. Besides that, it provides support for Git, Apache Maven, Gradle, etc.

Features of Eclipse

  • It allows setting breakpoints.
  • Automatically validates syntax.
  • Offers a robust debugger.
  • Provides you with readymade code templates.
  • Robust java editor.
  • It supports code refactoring.
  • It supports syntax coloring.

 To find out more about Eclipse and its setup process, you can have a look here.

MongoDB

MongoDB is an open-source platform that is a document-oriented programming language (NoSQL database). And it is the leading database in NoSQL. MongoDB is written in c++. As it is a NoSQL database, it uses JSON-like documents to store the data in the database.

Advantages of MongoDB

  • It is easy to set up MongoDB on your PC.
  • MongoDB is a schema-less database, so there won’t be any problem with Schema Migration.
  • It is very easily scalable.
  • It helps in providing fast access to data because of its nature of implementing internal memory to store the data.
  • As it is a document-oriented language, document queries are used, which plays a vital role in supporting dynamic queries.
  • As compared to other relational databases, it is easy to have performance tuning.

To know more about Mongo DB and its setup process, click here.

Mongo-Java-Driver

There are many drivers for MongoDB, in that Mongo-Java-Driver is one of the drivers, which is the official Mongo Driver used for synchronous Java applications. The MongoDB Java driver API documentation contains several libraries organized by functionality. For detailed information about classes and methods in each library, see the following table for their descriptions and links to the API documentation. To find out more about Mongo-Java-Driver and its setup process, visit this tutorial.

How to use MongoDB in Eclipse

To use MongoDB in Eclipse, you have to install MongoDB. Eclipse has to be installed in the system. If you haven’t installed them,

  1. Click here to Download MongoDB.
  2. Click here to Download Eclipse IDE.
  • And you have to install the driver which connects both MongoDB and java if you are using java as a programming language for coding your database.
  • For example, if you want to use a python programming language to code your database, you need to download and install pymongo as a driver.
  • Here in this article, I am going to use Java programming language for demonstration, so I am using mongo-java-driver.  

Make sure that you keep MongoDB always open during the execution of the program.

  • Open the command prompt and type mongo (this is to verify whether MongoDB is installed on your PC or not).
Verify-the-mongoDB-installation

If this is shown, MongoDB has been installed on your PC

  • If MongoDB is not properly installed on your PC, you will get an error like this.
Error-case

You will have an error as the mongo keyword is not recognized

  • If everything is installed well, then, Open Eclipse and create a new Java project with any name (here I am creating with MongoDB_with_Java) as mentioned in the image below.
Creating-new-java-project

 

  • A project with the name MongoDB_with_Java would be created.
  • Right-click on project MongoDB_with_Java and click on properties. (Or click on the project name and press Alt+Enter to open properties).
Opening-properties

 

  •  Click on Java Build Path, and go to libraries.
Go-to-libraries

 

  • Click on Add External JAR and add mongo-java-driver from where you have downloaded it.
  • Now click on Apply and close.
  • Now right, click on the MongoDB_with_Java project and create a class.
  • Add all the necessary libraries in the class to write the java program to use the Mongo DB in eclipse.

Example program for creating a database with MongoDB in eclipse.

Java




import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient; //including essential libraries
  
public class demo {
  
    public static void main(String[] args) throws Exception
    {
        // throwing an exception
        try {
            MongoClient mongoClient = new MongoClient("localhost",27017); // establishing a server connection in localhost
            DB db = mongoClient.getDB("student"); // creating database called student
            System.out.println("connection established"); // prints connection established if database created
            DBCollection coll = db.getCollection("student");
        }
        catch (Exception e) {
            System.out.println("error"); // if the exception occurs prints error
        }
        System.out.println("server ready"); // if there is no exception prints server ready
    }
}


We will establish a server connection with localhost, and then we will create a database with the name Student. If the database is created, then it will give output as the database created, and if an exception occurs, it will print an error.

Database-output-created

 

So that we can create a new database, manage the database, and do all the operations on the database in eclipse using MongoDB. You can learn more about MongoDB by following this Mongo DB tutorial.



Last Updated : 20 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads