Wednesday, September 25, 2019

Java connecting to MongoDB database examples

1. Downloading Mongo Java Driver

Click here to download latest version of Mongo Java Driver (version 2.11.1 as of this writing). The JAR file name is mongo-java-driver-VERSION.jar (around 400KB). Copy the downloaded JAR file into your classpath.
Online API documentation for Mongo Java Driver can be found here.


2. Connecting  to MongoDB using MongoClient

The MongoClientclass is used to make a connection with a MongoDB server and perform database-related operations. Here are some examples:
  • Creating a MongoClient instance that connects to a default MongoDB server running on localhost and default port:
    1
    MongoClient mongoClient = new MongoClient();
  • Connecting to a named MongoDB server listening on the default port (27017):
    1
    MongoClient mongoClient = new MongoClient("localhost");
    Or:
    1
    MongoClient mongoClient = new MongoClient("db1.server.com");
  • Connecting to a named MongoDB server listening on a specific port:
    1
    MongoClient mongoClient = new MongoClient("localhost"27017);
    Or:
    1
    MongoClient mongoClient = new MongoClient("db1.server.com"27018);
  • Connecting to a replica set of servers:
1
2
3
4
5
6
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add(new ServerAddress("db1.server.com"27017));
seeds.add(new ServerAddress("db2.server.com"27018));
seeds.add(new ServerAddress("db3.server.com"27019));
 
MongoClient mongoClient = new MongoClient(seeds);

After the connection is established, we can obtain a database and make authentication (if the server is running in secure mode), for example:
1
2
3
4
5
6
7
8
9
10
11
MongoClient mongoClient = new MongoClient();
DB db = mongoClient.getDB("test");
 
char[] password = new char[] {'s''e''c''r''e''t'};
boolean authenticated = db.authenticate("root", password);
 
if (authenticated) {
    System.out.println("Successfully logged in to MongoDB!");
else {
    System.out.println("Invalid username/password");
}
By default, MongoDB server is running in trusted mode which doesn’t require authentication.

Let’s see a complete program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package net.codejava.mongodb;
 
import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;
 
import com.mongodb.DB;
import com.mongodb.MongoClient;
 
public class JavaMongoDBConnection {
 
    public static void main(String[] args) {
        try {
             
            MongoClient mongoClient = new MongoClient("localhost");
             
            List<String> databases = mongoClient.getDatabaseNames();
             
            for (String dbName : databases) {
                System.out.println("- Database: " + dbName);
                 
                DB db = mongoClient.getDB(dbName);
                 
                Set<String> collections = db.getCollectionNames();
                for (String colName : collections) {
                    System.out.println("\t + Collection: " + colName);
                }
            }
             
            mongoClient.close();
             
        catch (UnknownHostException ex) {
            ex.printStackTrace();
        }
         
    }
}
This Java program connects to a MongoDB server running on localhost at default port, then lists all database names available on the server. For each database, it lists all collection names (a collection is equivalent to a table in relational database), and finally closes the connection. This program would produce the following output:
- Database: local
       + Collection: startup_log
- Database: mydb
       + Collection: system.indexes
       + Collection: things
- Database: test
       + Collection: system.indexes
       + Collection: test


3. Using MongoDB connection string URI

It’s also possible to use a String that represents a database connection URI to connect to the MongoDB server, for example:
1
2
String dbURI = "mongodb://localhost";
MongoClient mongoClient = new MongoClient(new MongoClientURI(dbURI));
Syntax of the URI is as follows:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Here are some connection string URI examples:
  • Connecting to the MongoDB server running on localhost at the default port:
mongodb://localhost
  • Connecting to the admin database on a named MongoDB server db1.server.com running on port 27027 with user root and password secret:
mongodb://root:secret@db1.server.com:27027
  • Connecting to the users database on server db2.server.com:
mongodb://db2.server.com/users
  • Connecting to the products database on a named MongoDB server db3.server.com running on port 27027 with user tom and password secret:
mongodb://tom:secret@db3.server.com:27027/products
  • Connecting to a replica set of three servers:
mongodb://db1.server.com,db2.server.com,db3.server.com

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...