Java Connection to MongoDB
MongoDB is one of the most popular NoSQL databases, which can be easily used within the platform. In this guide we’ll show you how to connect this instance with your app and perform some simple operations.
Create Environment
1. Log in to your PaaS account.
2. Create an environment with MongoDB instance (available within the NoSQL section). The preferred application server for your app to be deployed to can be located in the same or separate environment (as an example, we’ve added Tomcat):
Click Create and wait a couple of minutes for your environment to appear at the dashboard.
MongoDB Configurations
1. Check your email inbox - it should contain a message with the database details:
2. Click the Open in browser button for the MongoDB node within your dashboard or use the Access URL from the email to access the database admin panel.
Log in using the abovementioned credentials.
3. Create a new database by switching to the Databases tab and typing a Name for it (e.g. mongodb-connect) within the Create Database form.
Click Save to continue.
4. Next, you need to create a user for this DB - thus, navigate to the Execute tab and choose your newly created database via the corresponding drop-down list.
Then type the following command within the input frame above:
db.createUser({ user: “user_name”, pwd: “password”, roles:[{ role: “readWrite”, db: “db_name"}]})
where:
- user_name - desired name of your new DB user
- password - password for this user
- db_name - name of the database the newly created user will get the read/write permissions for
After that, click the Execute button and wait for the success response.
5. Now, return to the dashboard and create a separate file for storing database connection information.
Click the Config button next to your application server in the corresponding environment (Tomcat in our case).
In the opened tab, create a new mydb.cfg file inside the home directory and specify the following strings there:
|
|
where
- {db_access_url} - link to the database admin panel (find it within the corresponding email or click Open in browser next to your MongoDB node and copy it from the address bar) without https:// part
- {db_name} - name of the created database (mongodb-connect in our case)
- {user_name} - name of the DB user you’ve assigned to this database (dbuser in our case)
- {password} - the corresponding user’s password
Do not forget to Save the changes.
Application Deployment
1. Now you can deploy your project to the prepared environment.
As an example, here is the code of our application, intended to test the connection to our MongoDB node.
MongoManager.java
|
|
2. Any application can be deployed using either platform Deployment Manager (with a local file/URL as a source) or Maven (for deployment from GIT/SVN).
For testing, you can try our ready-to-use mongoclient.war project, which already contains the appropriate connector driver (or download its sources and adjust it in any preferred way).
3. As a result, you’ll get an environment similar to the one below:
Connection Check Up
1. Click Open in Browser next to your environment with our example app deployed. You’ll see a new window opened with the simple MongoDB Manager form shown.
Type the desired number of rows (for being added to the corresponding database) into the appropriate field and click the Insert rows button.
2. Wait a moment until the page stops updating and return to the MongoDB admin panel.
Navigate to the mongodb-connect database within the list to the left and check for the new mycollection collection inside - it should include the above stated amount of records, 5 in our case.
As you can see, everything works fine, as an application could connect to our DB. Now you can use the admin panel to perform any other required operations to your database in a similar way.
Useful to Know
The platform documentation also contains a number of auxiliary guides for the MongoDB server, that may become useful even for the subject-familiar and experienced users:
- configure a Replica Set with an uneven number of MongoDB nodes to simultaneously implement the master-slave replication and automated failover
- ensure information safety through adjusting the Backups Scheduling - this prevents data loss and allows to restore it in case of an unexpected server failure
- learn how to establish the Remote Access to your database for getting the ability to work with it via any suitable client application, even without the necessity to log in to our dashboard
- read the Dump Import/Export guide to discover how to manually backup your data within dump files and subsequently restore it from the previously created dumps if necessary