Skip to content

Commit 7fa3ffd

Browse files
authored
Merge pull request #23 from mongodben/DOCSP-29142
(DOCSP-29142): Kotlin Driver Quick Start page
2 parents 2be3bc0 + 3d53303 commit 7fa3ffd

File tree

9 files changed

+329
-53
lines changed

9 files changed

+329
-53
lines changed

examples/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
val kotlin_mongodb_version: String by project
2+
13
plugins {
24
kotlin("jvm") version "1.8.0"
35
id("com.google.osdetector") version "1.7.3"
@@ -13,6 +15,8 @@ repositories {
1315
}
1416

1517
dependencies {
18+
implementation("org.mongodb:mongodb-driver-kotlin-coroutine:$kotlin_mongodb_version")
19+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
1620
testImplementation(kotlin("test"))
1721
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.0-Beta")
1822
implementation("org.mongodb:mongodb-driver-kotlin-coroutine:4.10.0-SNAPSHOT")

examples/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
kotlin.code.style=official
2+
kotlin_mongodb_version=4.10.0-SNAPSHOT
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.mongodb.docs.kotlin
2+
// :snippet-start: quick-start-data-class
3+
import com.mongodb.client.model.Filters.eq
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import io.github.cdimascio.dotenv.dotenv
6+
import kotlinx.coroutines.flow.firstOrNull
7+
import kotlinx.coroutines.runBlocking
8+
9+
// Create data class to represent a MongoDB document
10+
data class Movie(val title: String, val year: Int, val cast: List<String>)
11+
12+
fun main() {
13+
// :remove-start:
14+
val dotenv = dotenv()
15+
val CONNECTION_STRING_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
16+
// :remove-end:
17+
18+
// Replace the placeholder with your MongoDB deployment's connection string
19+
val uri = CONNECTION_STRING_URI_PLACEHOLDER
20+
21+
val mongoClient = MongoClient.create(uri)
22+
val database = mongoClient.getDatabase("sample_mflix")
23+
// Get a collection of documents of type Movie
24+
val collection = database.getCollection<Movie>("movies")
25+
26+
runBlocking {
27+
val doc = collection.find(eq("title", "Back to the Future")).firstOrNull()
28+
if (doc != null) {
29+
println(doc)
30+
} else {
31+
println("No matching documents found.")
32+
}
33+
}
34+
35+
mongoClient.close()
36+
}
37+
38+
// :snippet-end:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.mongodb.docs.kotlin
2+
// :snippet-start: quick-start-document
3+
import com.mongodb.client.model.Filters.eq
4+
import com.mongodb.kotlin.client.coroutine.MongoClient
5+
import io.github.cdimascio.dotenv.dotenv
6+
import kotlinx.coroutines.flow.firstOrNull
7+
import kotlinx.coroutines.runBlocking
8+
import org.bson.Document
9+
10+
fun main() {
11+
// :remove-start:
12+
val dotenv = dotenv()
13+
val CONNECTION_STRING_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
14+
// :remove-end:
15+
16+
// Replace the placeholder with your MongoDB deployment's connection string
17+
val uri = CONNECTION_STRING_URI_PLACEHOLDER
18+
19+
val mongoClient = MongoClient.create(uri)
20+
val database = mongoClient.getDatabase("sample_mflix")
21+
val collection = database.getCollection<Document>("movies")
22+
23+
runBlocking {
24+
val doc = collection.find(eq("title", "Back to the Future")).firstOrNull()
25+
if (doc != null) {
26+
println(doc.toJson())
27+
} else {
28+
println("No matching documents found.")
29+
}
30+
}
31+
32+
mongoClient.close()
33+
}
34+
35+
// :snippet-end:

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ version = "4.9"
2121
full-version = "{+version+}.1"
2222
version-next = "4.10"
2323
mdb-server = "MongoDB server"
24+
kotlin-docs = "https://kotlinlang.org/docs"
2425

2526
package-name-org = "mongodb-org"
2627
api = "https://mongodb.github.io/mongo-java-driver/{+version+}"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import com.mongodb.client.model.Filters.eq
2+
import com.mongodb.kotlin.client.coroutine.MongoClient
3+
import io.github.cdimascio.dotenv.dotenv
4+
import kotlinx.coroutines.flow.firstOrNull
5+
import kotlinx.coroutines.runBlocking
6+
7+
// Create data class to represent a MongoDB document
8+
data class Movie(val title: String, val year: Int, val cast: List<String>)
9+
10+
fun main() {
11+
12+
// Replace the placeholder with your MongoDB deployment's connection string
13+
val uri = CONNECTION_STRING_URI_PLACEHOLDER
14+
15+
val mongoClient = MongoClient.create(uri)
16+
val database = mongoClient.getDatabase("sample_mflix")
17+
// Get a collection of documents of type Movie
18+
val collection = database.getCollection<Movie>("movies")
19+
20+
runBlocking {
21+
val doc = collection.find(eq("title", "Back to the Future")).firstOrNull()
22+
if (doc != null) {
23+
println(doc)
24+
} else {
25+
println("No matching documents found.")
26+
}
27+
}
28+
29+
mongoClient.close()
30+
}
31+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import com.mongodb.client.model.Filters.eq
2+
import com.mongodb.kotlin.client.coroutine.MongoClient
3+
import io.github.cdimascio.dotenv.dotenv
4+
import kotlinx.coroutines.flow.firstOrNull
5+
import kotlinx.coroutines.runBlocking
6+
import org.bson.Document
7+
8+
fun main() {
9+
10+
// Replace the placeholder with your MongoDB deployment's connection string
11+
val uri = CONNECTION_STRING_URI_PLACEHOLDER
12+
13+
val mongoClient = MongoClient.create(uri)
14+
val database = mongoClient.getDatabase("sample_mflix")
15+
val collection = database.getCollection<Document>("movies")
16+
17+
runBlocking {
18+
val doc = collection.find(eq("title", "Back to the Future")).firstOrNull()
19+
if (doc != null) {
20+
println(doc.toJson())
21+
} else {
22+
println("No matching documents found.")
23+
}
24+
}
25+
26+
mongoClient.close()
27+
}
28+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.. _fundamentals-data-classes:
2+
3+
==================================
4+
Document Data Format: Data Classes
5+
==================================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
15+
Overview
16+
--------
17+
18+
TODO: add content. right now making a container page for the ref used in the quickstart

0 commit comments

Comments
 (0)