This repository contains an example application that connects to a MongoDB instance using the Java MongoDB Driver. You can use this example application as a starting point for a Java application that connects to MongoDB Atlas.
-
To build and run this project, you will need IntelliJ IDEA Community Edition for MacOS, Windows, or Linux. You can download it here.
-
A working installation of the Java 8 JDK.
-
A working installation of the Maven build system. (Your IntelliJ installation should include this)
It is also helpful, but not necessary, to have a working installation of Git version control.
Some of these tools may come pre-installed in your programming environment.
The following instructions explain how to get this project connected to your instance of MongoDB Atlas.
To get started with this sample project, download this repository to your programming environment. You can either download this project using Git version control:
git clone [email protected]:mongodb-university/atlas_starter_java.git
Or you can download a ZIP archive using your browser from GitHub. If you download this project as a ZIP archive, unzip the archive before proceeding.
-
In IntelliJ, select
File > Open...
-
Navigate to the directory containing this project.
-
Select the directory
atlas_starter_java
. -
Click
OK
.
-
In IntelliJ, click to expand the
atlas_starter_java
directory. -
Right click on the directory named
src
. -
Select
Mark Directory as
from the context menu. -
Select
Sources Root
.
-
Expand
atlas_starter_java > src > main > java > mongodb > Main
. -
Double-click
Main
. -
On the following lines in
Main
, replace the Atlas connection URI with your own from the Atlas UI.
// TODO:
// Replace the placeholder connection string below with your
// Altas cluster specifics. Be sure it includes
// a valid username and password! Note that in a production environment,
// you do not want to store your password in plain-text here.
String mongoUri = "<Your Atlas Connection String>";
- Replace the
<password>
section of the Atlas connection URI you just pasted intoMain
with your password!
-
Right click
Main
. -
Select
Run 'Main.main()'
from the context menu.
Congratulations! You have just connected to MongoDB Atlas using the Java MongoDB Driver! Try modifying the code to experiment with the Driver and MongoDB!
Are you having trouble getting connected to your MongoDB Atlas instance? Double-check the following:
-
Have you replaced the
mongoUri
variable with a valid connection string provided by the Atlas UI? -
Have you whitelisted your current IP address in the Atlas UI?
-
Do you have a working installation of Java?