Class MongoClient

java.lang.Object
com.codename1.backend.mongo.MongoClient
All Implemented Interfaces:
Closeable, AutoCloseable

public final class MongoClient extends Object implements Closeable

A pool of connections to one MongoDB deployment.

MongoClient mongo = MongoClient.current();
MongoCollection users = mongo.database().collection("users");
users.insertOne(doc);
List found = users.find(Filters.eq("email", email));
Setting
cn1.mongodb.uri the connection string; MONGODB_URI in the environment

Without cn1.mongodb.uri, Spring Boot's spring.data.mongodb.uri is read, so a Spring configuration carries over. The database is the one the URI names, or spring.data.mongodb.database, or test as the official drivers default.

Calls borrow a connection, use it and give it back. With none idle another is opened rather than making the caller wait, and at most maxPoolSize (16) are kept idle. Hosts are tried in order and the first that answers is used; of several, the first that is the replica set's primary.

  • Method Details

    • open

      public static MongoClient open(String connectionString) throws IOException
      A client for this connection string; the caller closes it.
      Throws:
      IOException
    • open

      public static MongoClient open(Config config) throws IOException
      A client for the deployment config names; the caller closes it.
      Throws:
      IOException
    • current

      public static MongoClient current() throws IOException
      The client of the calling thread's server: opened from its configuration the first time it is asked for, and closed when the server stops.
      Throws:
      IOException
    • database

      public MongoDatabase database()
      The database the connection string names.
    • database

      public MongoDatabase database(String name)
      A database by name.
    • runCommand

      public Map runCommand(String database, Map command) throws IOException
      Runs one command against database on a pooled connection.
      Throws:
      IOException
    • close

      public void close()
      Closes every idle connection; ones in use close when they are returned.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable