Connect to MongoDB
Create the Application File
Create a file called DemoDataClassExample.kt
in your project.
Copy the following sample code into the file and replace the value of
the <connection URI string>
placeholder with your MongoDB
Atlas connection string that you saved in the preceding step.
import com.mongodb.client.model.Filters.eq import com.mongodb.kotlin.client.MongoClient // Create data class to represent a MongoDB document data class Movie(val title: String, val year: Int, val directors: List<String>) fun main() { // Replace the placeholder with your MongoDB deployment's connection string val uri = "<connection URI string>" val mongoClient = MongoClient.create(uri) val database = mongoClient.getDatabase("sample_mflix") val collection = database.getCollection<Movie>("movies") // Find a document with the specified title val doc = collection.find(eq(Movie::title.name, "Before Sunrise")).firstOrNull() if (doc != null) { // Print the matching document println(doc) } else { println("No matching documents found.") } }
Note
This example uses a Kotlin data class to model MongoDB data.
Run the Application
When you run the application, it prints the details of a movie document that matches the query, as shown in the following output:
Movie(title=Before Sunrise, year=1995, directors=[Richard Linklater])
If you don't see any output or receive an error, check whether you included the proper connection string in your application. Also, confirm that you successfully loaded the sample dataset into your MongoDB Atlas cluster.
After completing this step, you have a working application that uses the Kotlin Sync driver to connect to your MongoDB cluster, run a query on the sample data, and print out the result.
Use the Document Class to Model Data (Alternative)
The preceding step demonstrates how to run a query on a sample collection to retrieve data by using a Kotlin data class. This section shows how to use the Document class to store and retrieve data from MongoDB.
In a file called DemoDocumentExample.kt
, paste the following sample
code to run a query on your sample dataset in MongoDB Atlas. Replace the
value of the <connection URI string>
placeholder with your
MongoDB Atlas connection string:
import com.mongodb.client.model.Filters.eq import com.mongodb.kotlin.client.MongoClient import org.bson.Document fun main() { // Replace the placeholder with your MongoDB deployment's connection string val uri = "<connection URI string>" val mongoClient = MongoClient.create(uri) val database = mongoClient.getDatabase("sample_mflix") val collection = database.getCollection<Document>("movies") // Find a document with the specified title val doc = collection.find(eq("title", "Before Sunrise")).firstOrNull() if (doc != null) { // Print the matching document println(doc) } else { println("No matching documents found.") } }
When you run the application, it prints the details of a movie document that matches the query, as shown in the following output:
Document{{_id=..., plot=A young man and woman ..., genres=[Drama, Romance], ...}}
If you don't see any output or receive an error, check whether you included the proper connection string in your application. Also, confirm that you successfully loaded the sample dataset into your MongoDB Atlas cluster.
After you complete these steps, you have a working application that uses the driver to connect to your MongoDB deployment, runs a query on the sample data, and prints out the result.
Note
If you run into issues on this step, ask for help in the MongoDB Community Forums or submit feedback by using the Rate this page tab on the right or bottom right side of this page.