Docs Menu
Docs Home
/ / /
Kotlin Sync Driver

Databases and Collections

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

MongoDB organizes data into a hierarchy of the following levels:

  • Databases: Top-level data structures in a MongoDB deployment that store collections.

  • Collections: Groups of MongoDB documents. They are analogous to tables in relational databases.

  • Documents: Units that store literal data such as string, numbers, dates, and other embedded documents. For more information about document field types and structure, see the Documents guide in the MongoDB Server manual.

To access a database, pass the database name to the getDatabase() method.

The following example accesses a database named test_database:

val db = client.getDatabase("test_database")

To access a collection, pass the database name to the getCollection() method.

The following example accesses a collection named test_collection:

val collection = db.getCollection("test_collection")

Tip

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

To explicitly create a collection in a MongoDB database, pass a collection name to the createCollection() method.

The following example creates a collection named example_collection:

db.createCollection("example_collection")

You can specify collection options, such as maximum size and document validation rules, by setting them in a CreateCollectionOptions instance. Then, pass the CreateCollectionOptions to the createCollection() method. For a full list of optional parameters, see the CreateCollectionOptions API documentation.

You can query for a list of collections in a database by calling the listCollections() method. The method returns a cursor containing all collections in the database and their associated metadata.

The following example calls the listCollections() method and iterates over the returned iterator to print the collections from the Access a Collection and Create a Collection examples:

val results = db.listCollections()
val jsonSettings = JsonWriterSettings.builder().indent(true).build()
results.forEach { result ->
println(result.toJson(jsonSettings))
}
{
"name": "example_collection",
"type": "collection",
"options": {},
"info": {
"readOnly": false,
...
},
"idIndex": { ... }
}
{
"name": "test_collection",
"type": "collection",
"options": {},
"info": {
"readOnly": false,
...
},
"idIndex": { ... }
}

To delete a collection from the database, call the drop() method on your collection.

The following example deletes the test_collection collection:

db.getCollection("test_collection").drop()

Warning

Dropping a Collection Deletes All Data in the Collection

Dropping a collection from your database permanently deletes all documents and all indexes within that collection.

Drop a collection only if you no longer need the data in it.

You can control how read and write operations run on replica sets by specifying a read preference, read concern, or write concern.

By default, databases inherit read and write settings from the MongoClient instance. Collections inherit these settings from the MongoClient or MongoDatabase instance on which the getCollection() method is called. You can change these settings by calling the following methods:

To learn more about setting a read preference, read concern, and write concern, see the Configure CRUD Operations guide.

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Connection Troubleshooting

On this page