How to Connect to a MongoDB Atlas Cluster in a Java Application
In this tutorial, we will see how to connect to a MongoDB Atlas cluster ina Java Application.
Your Java application must use the official MongoDB driver to connect to a MongoDB Atlas cluster.
You will add the MongoDB driver as dependency in your maven projects pom.xml
file.
<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.10.2</version>
</dependency>
You can check the latest version at https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync
To connect to MongoDB Atlas Cluster you need a valid connection String. The connection String is available in Atlas UI. You can get it by login to your Atlas account.
The format of the Connection String is as given below.
mongodb+srv://[username:password@]host[/[defaultauthdb][?options]]mongodb+srv://[username:password@]host[/[defaultauthdb][?options]]
For example,
mongodb+srv://user123:[email protected]/?retryWrites=true&w=majority
Here, user123
is username and password123
is a password.
Once you have a valid connection string, you can create a MongoClient using connection string and connect to Atlas cluster as shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.bson.Document;
import java.util.ArrayList;
import java.util.List;
public class ConnectToMongoDBCluster {
public static void main(String[] args) {
String connectionString = "mongodb+srv://user123:[email protected]/?retryWrites=true&w=majority";
try (MongoClient mongoClient = MongoClients.create(connectionString)) {
List<Document> databases = mongoClient.listDatabases().into(new ArrayList<>());
databases.forEach(db -> System.out.println(db.toJson()));
}
}
}
When you run the main class ConnectToMongoDBCluster
, it will connect to MongoDB Atlas cluster and print all the database.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!