Skip to content

MongoDatabase

A grouping of collections with the same theme.

What is a database?

Collections are grouped into databases to avoid name collisions. Databases are similar to Kotlin packages. If multiple applications are deployed in the same MongoDB instance in their own database, they can use the same collection names (e.g. users) without conflicts.

Each database has a name that must be unique within a MongoDB deployment.

Access

To obtain a database, see MongoClient.database.

To obtain a collection, see collection.

External resources

Properties

context

abstract val context: BsonContext

The full BSON configuration, used by the DSL to generate queries.

factory

abstract val factory: BsonFactory

The BsonFactory used to serialize and deserialize values.

name

abstract val name: String

The unique name of this database.

This name must be unique within a MongoDB deployment.

  • Two databases cannot have a name that only differs by case (e.g. salesData and SalesData cannot coexist).

  • Once a database is created, you must always access it with the same case as when it was created. The creation of a database happens on the first write operation in one of its collections.

  • It is recommended to avoid the following characters: /\. "$*<>:|?. Depending on the platform MongoDB is running on, some of them may be forbidden.

  • The name cannot be empty.

  • The name cannot be longer than 64 bytes.

External resources

objectIdGenerator

The algorithm used to generate new ObjectId instances.

propertyNameStrategy

The strategy used to convert property names to BSON document keys.

Functions

collection

Creates a MongoCollection object.

This method is purely a client-side operation, it does nothing in the MongoDB server. In MongoDB, databases and collections are created implicitly on the first insert.

For an example, see MongoClient.

Prefer using the overload that doesn't have a type argument. If type is specified, it must match Document. Otherwise, the behavior is unspecified.

Creates a MongoCollection object.

This method is purely a client-side operation, it does nothing in the MongoDB server. In MongoDB, databases and collections are created implicitly on the first insert.

For an example, see MongoClient.

collections

abstract fun collections(options: ListCollectionsOptions.() -> Unit = {}, filter: FilterQuery<CollectionInfo>.() -> Unit = {}): MongoIterable<CollectionInfo>

Lists the collections in this database.