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
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.

Click Create and wait for the environment to appear in the dashboard.
MongoDB Configurations
Check the MongoDB access details
After environment creation, check the email sent by the platform. It contains the database access details.

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.

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

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" }
]
})

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.

host={db_access_url}
dbname={db_name}
user={user_name}
password={password}
https:// prefix.mongodb-connect.
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.

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.

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

Enter the number of rows that should be inserted and click Insert rows.
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.

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
readWriterole 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.
