MongoDB Java Driver legacy all-in-one JAR providing comprehensive database connectivity for Java applications with synchronous APIs, CRUD operations, aggregation, GridFS, change streams, and authentication support
—
Database-level operations including collection management, command execution, and database administration with support for sessions and read/write preferences.
Main interface for database operations providing collection access, database management, and MongoDB command execution.
public interface MongoDatabase {
// Database metadata
String getName();
CodecRegistry getCodecRegistry();
ReadPreference getReadPreference();
WriteConcern getWriteConcern();
ReadConcern getReadConcern();
// Configuration variants
MongoDatabase withCodecRegistry(CodecRegistry codecRegistry);
MongoDatabase withReadPreference(ReadPreference readPreference);
MongoDatabase withWriteConcern(WriteConcern writeConcern);
MongoDatabase withReadConcern(ReadConcern readConcern);
// Collection access
MongoCollection<Document> getCollection(String collectionName);
<TDocument> MongoCollection<TDocument> getCollection(String collectionName, Class<TDocument> documentClass);
// Collection management
void createCollection(String collectionName);
void createCollection(String collectionName, CreateCollectionOptions options);
void createCollection(ClientSession clientSession, String collectionName);
void createCollection(ClientSession clientSession, String collectionName, CreateCollectionOptions options);
// View management
void createView(String viewName, String viewOn, List<? extends Bson> pipeline);
void createView(String viewName, String viewOn, List<? extends Bson> pipeline, CreateViewOptions options);
void createView(ClientSession clientSession, String viewName, String viewOn, List<? extends Bson> pipeline);
void createView(ClientSession clientSession, String viewName, String viewOn, List<? extends Bson> pipeline, CreateViewOptions options);
// Collection listing
MongoIterable<String> listCollectionNames();
MongoIterable<String> listCollectionNames(ClientSession clientSession);
ListCollectionsIterable<Document> listCollections();
<TResult> ListCollectionsIterable<TResult> listCollections(Class<TResult> resultClass);
ListCollectionsIterable<Document> listCollections(ClientSession clientSession);
<TResult> ListCollectionsIterable<TResult> listCollections(ClientSession clientSession, Class<TResult> resultClass);
// Command execution
Document runCommand(Bson command);
Document runCommand(Bson command, ReadPreference readPreference);
<TResult> TResult runCommand(Bson command, Class<TResult> resultClass);
<TResult> TResult runCommand(Bson command, ReadPreference readPreference, Class<TResult> resultClass);
Document runCommand(ClientSession clientSession, Bson command);
Document runCommand(ClientSession clientSession, Bson command, ReadPreference readPreference);
<TResult> TResult runCommand(ClientSession clientSession, Bson command, Class<TResult> resultClass);
<TResult> TResult runCommand(ClientSession clientSession, Bson command, ReadPreference readPreference, Class<TResult> resultClass);
// Database management
void drop();
void drop(ClientSession clientSession);
// Change streams
ChangeStreamIterable<Document> watch();
<TResult> ChangeStreamIterable<TResult> watch(Class<TResult> resultClass);
ChangeStreamIterable<Document> watch(List<? extends Bson> pipeline);
<TResult> ChangeStreamIterable<TResult> watch(List<? extends Bson> pipeline, Class<TResult> resultClass);
ChangeStreamIterable<Document> watch(ClientSession clientSession);
<TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, Class<TResult> resultClass);
ChangeStreamIterable<Document> watch(ClientSession clientSession, List<? extends Bson> pipeline);
<TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass);
// Database-level aggregation
AggregateIterable<Document> aggregate(List<? extends Bson> pipeline);
<TResult> AggregateIterable<TResult> aggregate(List<? extends Bson> pipeline, Class<TResult> resultClass);
AggregateIterable<Document> aggregate(ClientSession clientSession, List<? extends Bson> pipeline);
<TResult> AggregateIterable<TResult> aggregate(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass);
}
// View creation options
public class CreateViewOptions {
public CreateViewOptions collation(Collation collation);
public Collation getCollation();
}Creating and managing MongoDB collections and views within databases.
Usage Examples:
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.client.model.ValidationOptions;
import com.mongodb.client.model.Filters;
// Create a simple collection
database.createCollection("users");
// Create collection with options
CreateCollectionOptions options = new CreateCollectionOptions()
.capped(true)
.sizeInBytes(1000000)
.maxDocuments(1000);
database.createCollection("logs", options);
// Create a view
database.createView("activeUsers", "users", Arrays.asList(
Aggregates.match(Filters.eq("status", "active"))
));Direct execution of MongoDB database commands with flexible result handling.
Usage Examples:
import org.bson.Document;
// Execute a ping command
Document result = database.runCommand(new Document("ping", 1));
// Execute with read preference
Document stats = database.runCommand(
new Document("dbStats", 1),
ReadPreference.secondary()
);
// Execute with custom result class
MyCustomResult result = database.runCommand(
new Document("customCommand", 1),
MyCustomResult.class
);Run aggregation pipelines at the database level for operations that don't require a specific collection.
Usage Examples:
import com.mongodb.client.model.Aggregates;
// List current operations
AggregateIterable<Document> currentOps = database.aggregate(Arrays.asList(
new Document("$currentOp", new Document())
));
// List local sessions
AggregateIterable<Document> sessions = database.aggregate(Arrays.asList(
new Document("$listLocalSessions", new Document())
));Install with Tessl CLI
npx tessl i tessl/maven-org-mongodb--mongo-java-driver