Skip to content

MongoDatabase

interface 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

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

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.