Class MongoClient
java.lang.Object
com.codename1.backend.mongo.MongoClient
- All Implemented Interfaces:
Closeable, AutoCloseable
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 Summary
Modifier and TypeMethodDescriptionvoidclose()Closes every idle connection; ones in use close when they are returned.static MongoClientcurrent()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.database()The database the connection string names.A database by name.static MongoClientA client for the deploymentconfignames; the caller closes it.static MongoClientA client for this connection string; the caller closes it.runCommand(String database, Map command) Runs one command againstdatabaseon a pooled connection.
-
Method Details
-
open
A client for this connection string; the caller closes it.- Throws:
IOException
-
open
A client for the deploymentconfignames; the caller closes it.- Throws:
IOException
-
current
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
The database the connection string names. -
database
A database by name. -
runCommand
Runs one command againstdatabaseon a pooled connection.- Throws:
IOException
-
close
public void close()Closes every idle connection; ones in use close when they are returned.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-