Java Connection to MongoDB

Java Connection to MongoDB

This guide shows how to connect a Java application to MongoDB on the platform, create a database and database user, store the connection settings, deploy a Java application, and verify the connection by inserting records into a MongoDB collection.

Create the Environment

1

Create a MongoDB environment

Create an environment with a MongoDB instance from the NoSQL section. The Java application server can be in the same environment or in a separate environment. The source example adds Tomcat.

Create MongoDB environment
Create an environment with MongoDB and, if required, a Java application server such as Tomcat.

Click Create and wait for the environment to appear in the dashboard.

MongoDB Configurations

1

Check the MongoDB access details

After environment creation, check the email sent by the platform. It contains the database access details.

MongoDB credentials email
The platform email contains the MongoDB access details.
2

Open the MongoDB administration panel

Click Open in Browser for the MongoDB node or use the Access URL from the email, then sign in with the supplied credentials.

MongoDB admin panel login
Open the MongoDB administration panel and sign in with the supplied credentials.
3

Create a database

Open the Databases tab, enter a database name, and save it. The source example uses mongodb-connect.

Create MongoDB database
Create a new MongoDB database from the Databases tab.
4

Create a database user

Open the Execute tab, select the newly created database, and create a user with read/write permissions.

db.createUser({
  user: "user_name",
  pwd: "password",
  roles: [
    { role: "readWrite", db: "db_name" }
  ]
})
user_nameName of the new database user.
passwordPassword for the new database user.
db_nameDatabase for which the user receives read/write permissions.
Execute MongoDB command
Use the Execute tab to create a database user with read/write access.

Store the MongoDB Connection Settings

The connection data can be included directly in the Java application, but the source example stores it in a separate file that is read by the test application.

Open Config for the Java application server and create mydb.cfg in its home directory.

Application server configuration
Open the Config panel for the Java application server.
host={db_access_url}
dbname={db_name}
user={user_name}
password={password}
{db_access_url}MongoDB administration-panel address without the https:// prefix.
{db_name}Name of the created database, such as mongodb-connect.
{user_name}Name of the database user assigned to the database.
{password}Password for that database user.
MongoDB connection data
Store the MongoDB connection values in mydb.cfg.

Save the configuration file.

Application Deployment

The source test application reads mydb.cfg, creates a MongoDB credential, connects through MongoClient, opens the selected database, works with a collection named mycollection, inserts timestamped documents, and then closes the client connection.

MongoCredential credential =
    MongoCredential.createCredential(
        user,
        dbname,
        password.toCharArray()
    );

MongoClient mongoClient =
    new MongoClient(
        new ServerAddress(host),
        Arrays.asList(credential)
    );

MongoDatabase db =
    mongoClient.getDatabase(dbname);

The Java application can be deployed through Deployment Manager using a local file or URL, or through Maven for a project stored in Git/SVN.

The source documentation also provides a ready-to-use mongoclient.war test project that already includes the required MongoDB connector.

Upload MongoDB Java application
Upload or deploy the Java test application.
i

Your own Java project

For a custom project, add the appropriate mongo-java-driver JAR library to webapps/{app_context}/WEB-INF/lib on the application server.

MongoDB Java application deployed
The Java application is deployed to the prepared environment.

Connection Check Up

1

Open the test application

Click Open in Browser for the environment where the example application is deployed. The MongoDB Manager form opens.

MongoDB Manager application
Use the test form to insert the requested number of rows.

Enter the number of rows that should be inserted and click Insert rows.

2

Verify the collection

Return to the MongoDB administration panel, open the mongodb-connect database, and check the mycollection collection. It should contain the number of records requested by the test application.

Check MongoDB collection
Verify that mycollection contains the inserted records.

Expected Result

The Java application reads the MongoDB connection information, authenticates with the configured database user, connects to the selected MongoDB database, inserts documents into mycollection, and the new records are visible in the MongoDB administration panel.

Important Notes

  • The MongoDB and Java application server can be deployed in the same or separate environments.
  • The source example uses Tomcat as the Java application server.
  • The example database is mongodb-connect.
  • The example database user receives the readWrite role for the selected database.
  • The source example stores connection settings in mydb.cfg.
  • Use the MongoDB access URL without the https:// prefix in the documented configuration.
  • Custom Java applications require the appropriate MongoDB Java driver in the application library folder.