8000 Merge branch 'master' into DOCSP-29185 · nickldp/docs-kotlin@ae80717 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae80717

Browse files
authored
Merge branch 'master' into DOCSP-29185
2 parents 4c5c199 + 7fa3ffd commit ae80717

File tree

105 files changed

+2811
-626
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+2811
-626
lines changed

examples/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ bin/
3939
.vscode/
4040

4141
### Mac OS ###
42-
.DS_Store
42+
.DS_Store
43+
44+
### mlaunch ###
45+
/data/

examples/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Kotlin Driver Code Examples
2+
3+
This directory contains the tested code examples for the Kotlin Driver.
4+
5+
## Set up
6+
7+
1. Use IntelliJ for testing.
8+
1. Create a MongoDB cluster running >=6.0.0.
9+
1. Create a `.env` file.
10+
1. Then add the connection URI to a `MONGODB_CONNECTION_URI` variable to the `.env` file.
11+
12+
## Adding Code Examples to the Docs
13+
14+
The tests use [Bluehawk](https://mongodb-university.github.io/Bluehawk/)
15+
to extract the code examples from the source file.
16+
17+
You can generate all the code examples by running the `bluehawk.sh` script.
18+
19+
## Setting up the MongoDB Cluster
20+
21+
Currently, it does not matter if you are running a cluster in Atlas or locally.
22+
(This might change to be Atlas-only, at least for certain tests, if there are code
23+
examples added for Atlas-only features like Atlas Search.)
24+
25+
If you are using Atlas, create a cluster and connect to it in the standard way.
26+
Most tests can run on an M0 (free/shared), but you **must** use a dedicated cluster (>=M10)
27+
for the certain tests, such as those for document pre-images in collections.
28+
29+
You can use [mlaunch](https://rueckstiess.github.io/mtools/mlaunch.html)
30+
to spin up a replica set cluster locally with the command:
31+
32+
```sh
33+
mlaunch --replicaset --nodes 3
34+
```
35+
36+
Stop the cluster with:
37+
38+
```sh
39+
mlaunch stop
40+
```
41+
42+
Restart it with:
43+
44+
```sh
45+
mlaunch start
46+
```
47+
48+
Refer to the mlaunch docs for install info and other commands.

examples/build.gradle.kts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
val kotlin_mongodb_version: String by project
2+
13
plugins {
24
kotlin("jvm") version "1.8.0"
5+
id("com.google.osdetector") version "1.7.3"
36
application
47
}
58

@@ -12,14 +15,18 @@ repositories {
1215
}
1316

1417
dependencies {
18+
implementation("org.mongodb:mongodb-driver-kotlin-coroutine:$kotlin_mongodb_version")
19+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
1520
testImplementation(kotlin("test"))
1621
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.0-Beta")
1722
implementation("org.mongodb:mongodb-driver-kotlin-coroutine:4.10.0-SNAPSHOT")
1823
implementation("org.mongodb:mongodb-driver-core:4.10.0-SNAPSHOT")
1924
implementation("org.slf4j:slf4j-api:1.7.32")
2025
implementation("ch.qos.logback:logback-classic:1.2.6")
2126
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
22-
27+
implementation("org.apache.logging.log4j:log4j-slf4j-impl:2.17.1")
28+
implementation("io.netty:netty-all:4.1.91.Final")
29+
implementation("io.netty:netty-tcnative-boringssl-static:2.0.53.Final:${osdetector.classifier}")
2330
}
2431

2532
tasks.test {
@@ -32,4 +39,4 @@ kotlin {
3239

3340
application {
3441
mainClass.set("MainKt")
35-
}
42+
}

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:
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
import com.mongodb.*
3+
import com.mongodb.client.model.Filters.*
4+
import com.mongodb.client.model.Projections.*
5+
import com.mongodb.kotlin.client.coroutine.MongoClient
6+
import io.github.cdimascio.dotenv.dotenv
7+
import kotlinx.coroutines.flow.first
8+
import kotlinx.coroutines.runBlocking
9+
import org.bson.BsonObjectId
10+
import org.bson.Document
11+
import org.bson.codecs.pojo.annotations.BsonId
12+
import org.junit.jupiter.api.AfterAll
13+
import org.junit.jupiter.api.Assertions.*
14+
import org.junit.jupiter.api.BeforeAll
15+
import org.junit.jupiter.api.TestInstance
16+
import java.util.*
17+
import kotlin.test.*
18+
19+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
20+
internal class BuildersTest {
21+
22+
// :snippet-start: user-data-class
23+
data class User(
24+
@BsonId
25+
val id: BsonObjectId = BsonObjectId(),
26+
val gender: String,
27+
val age: Int,
28+
val email: String,
29+
)
30+
// :snippet-end:
31+
32+
companion object {
33+
val dotenv = dotenv()
34+
val client = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"])
35+
val database = client.getDatabase("marketing")
36+
val collection = database.getCollection<User>("users")
37+
38+
@BeforeAll
39+
@JvmStatic
40+
private fun beforeAll() {
41+
runBlocking {
42+
val users = listOf(
43+
User(BsonObjectId(), "female", 29, "baz@example.com"),
44+
User(BsonObjectId(), "male", 92, "bar@example.com"),
45+
User(BsonObjectId(), "female", 35, "foo@example.com"),
46+
)
47+
collection.insertMany(users)
48+
}
49+
}
50+
51+
@AfterAll
52+
@JvmStatic
53+
private fun afterAll() {
54+
runBlocking {
55+
collection.drop()
56+
client.close()
57+
}
58+
}
59+
60+
}
61+
62+
@Test
63+
fun noBuildersTest() = runBlocking {
64+
// :snippet-start: no-builders
65+
data class Results(val email: String)
66+
67+
val filter = Document().append("gender", "female").append("age", Document().append("\$gt", 29))
68+
val projection = Document().append("_id", 0).append("email", 1)
69+
val results = collection.find<Results>(filter).projection(projection)
70+
// :snippet-end:
71+
assertEquals("foo@example.com", results.first().email)
72+
}
73+
74+
@Test
75+
fun buildersTest() = runBlocking {
76+
// :snippet-start: builders
77+
data class Results(val email: String)
78+
79+
val filter = and(eq(User::gender.name, "female"), gt(User::age.name, 29))
80+
val projection = fields(excludeId(), include("email"))
81+
val results = collection.find<Results>(filter).projection(projection)
82+
// :snippet-end:
83+
assertEquals("foo@example.com", results.first().email)
84+
}
85+
}

0 commit comments

Comments
 (0)
0