Skip to content

CollectionOperations

Interface grouping MongoDB operations relating to collection administration.

Inheritors

Properties

context

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

For more information, see BsonContext.

Functions

create

abstract suspend fun create(options: CreateCollectionOptions<Document>.() -> Unit = {})

Explicitly creates this collection.

MongoDB will automatically create a collection with default settings on the first write operation. This method is useful to create a collection with non-default settings.

If this method is called and the collection already exists with the same options, nothing happens. If this method is called and the collection already exists with different options, an error is thrown.

Example

Create a capped collection: a collection with a maximum size on disk:

database.collection<Log>("logs")
    .create {
        capped(sizeBytes = 1024 * 1024) // 1 MiB
    }

External resources

drop

abstract suspend fun drop(options: DropOptions<Document>.() -> Unit = {})

Removes an entire collection from the database.

All documents within the collection are deleted.

Indexes attached to this collection are also deleted.

Example

collection.drop()

External resources