Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversJava Sync

Databases and Collections

On this page

  • Overview
  • Access a Database
  • Access a Collection
  • Create a Collection
  • Document Validation
  • Get a List of Collections
  • Drop a Collection
  • Specify Read Preferences, Read Concerns, and Write Concerns

In this guide, you can learn how to use MongoDB databases and collections with the MongoDB Java driver.

MongoDB organizes data into a hierarchy of the following levels:

  • Databases

  • Collections

  • Documents

Databases are the top level of data organization in a MongoDB instance. Databases are organized into collections which contain documents. Documents contain literal data such as strings, numbers, and dates as well as other (embedded) documents. For more information on document field types and structure, see the server documentation for documents.

Use the getDatabase() method of a MongoClient instance to access a MongoDatabase in a MongoDB instance.

The following example accesses a database named "testDatabase":

MongoDatabase database = mongoClient.getDatabase("testDatabase");

Use the getCollection() method of a MongoDatabase instance to access a MongoCollection in a database of your connected MongoDB instance.

The following example accesses a collection named "testCollection" from a MongoDatabase:

MongoDatabase database = mongoClient.getDatabase("testDatabase");
MongoCollection<Document> collection = database.getCollection("testCollection");

Tip

If the provided collection name does not already exist in the database, MongoDB implicitly creates the collection when you first insert data into that collection.

Use the createCollection() method of a MongoDatabase instance to create a collection in a database of your connected MongoDB instance.

The following example creates a collection called "exampleCollection":

database.createCollection("exampleCollection");

You can specify collection options like maximum size and document validation rules using the CreateCollectionOptions class. The createCollection() method accepts an instance of CreateCollectionOptions as an optional second parameter.

Document validation provides the ability to validate documents against a series of filters during writes to a collection. You can specify these filters using the ValidationOptions class, which accepts a series of Filters that specifies the validation rules and expressions:

ValidationOptions collOptions = new ValidationOptions().validator(
Filters.or(Filters.exists("commander"), Filters.exists("first officer")));
database.createCollection("ships",
new CreateCollectionOptions().validationOptions(collOptions));

For more information, see the server documentation for document validation.

You can query for a list of collections in a database using the MongoDatabase.listCollectionNames() method:

for (String name : database.listCollectionNames()) {
System.out.println(name);
}

You can remove a collection from the database using the MongoCollection.drop() method:

MongoCollection<Document> collection = database.getCollection("bass");
collection.drop();

Warning

Dropping a Collection Deletes All Data in the Collection

Dropping a collection from your database also permanently deletes all documents within that collection and all indexes on that collection. Only drop collections that contain data that is no longer needed.

Read preferences, read concerns, and write concerns control how the driver routes read operations and waits for acknowledgment for read and write operations when connected to a MongoDB replica set. Read preferences and read concerns apply to all read operations; write concerns apply to all write operations.

MongoDatabase instances inherit their write concern, read concern, and write preference settings from the MongoClient used to create them. MongoCollection instances inherit their write concern, read concern, and write preference settings from the MongoDatabase used to create them. However, you can use the following methods to obtain an instance of a MongoDatabase or MongoCollection with a read preference, read concern, or write concern that differs from the setting they would normally inherit:

Tip

The withReadConcern(), withReadPreference(), and withWriteConcern methods create a new instance of a MongoDatabase or MongoCollection with the desired preference or concern. The MongoDatabase or MongoCollection upon which the method is called retains its original preference and concern settings.

For more information, see the server documentation on read preferences, read concerns, and write concerns.

←  Stable APIData Formats →