Skip to content

Commit ed68cc4

Browse files
authored
Merge pull request #21 from mongodben/DOCSP-29202
(DOCSP-29202): Kotlin Builders page
2 parents 1d76b98 + 7c0f962 commit ed68cc4

File tree

5 files changed

+134
-27
lines changed

5 files changed

+134
-27
lines changed
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, "[email protected]"),
44+
User(BsonObjectId(), "male", 92, "[email protected]"),
45+
User(BsonObjectId(), "female", 35, "[email protected]"),
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("[email protected]", 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("[email protected]", results.first().email)
84+
}
85+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data class Results(val email: String)
2+
3+
val filter = and(eq(User::gender.name, "female"), gt(User::age.name, 29))
4+
val projection = fields(excludeId(), include("email"))
5+
val results = collection.find<Results>(filter).projection(projection)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data class Results(val email: String)
2+
3+
val filter = Document().append("gender", "female").append("age", Document().append("\$gt", 29))
4+
val projection = Document().append("_id", 0).append("email", 1)
5+
val results = collection.find<Results>(filter).projection(projection)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
data class User(
2+
@BsonId
3+
val id: BsonObjectId = BsonObjectId(),
4+
val gender: String,
5+
val age: Int,
6+
val email: String,
7+
)

source/fundamentals/builders.txt

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ Builders
77
.. toctree::
88

99
/fundamentals/builders/aggregates
10-
/fundamentals/builders/filters
11-
/fundamentals/builders/indexes
12-
/fundamentals/builders/projections
13-
/fundamentals/builders/sort
14-
/fundamentals/builders/updates
10+
11+
.. TODO: add these back after Must-Have phase complete
12+
.. /fundamentals/builders/filters
13+
.. /fundamentals/builders/indexes
14+
.. /fundamentals/builders/projections
15+
.. /fundamentals/builders/sort
16+
.. /fundamentals/builders/updates
1517

1618
.. contents:: On this page
1719
:local:
@@ -23,10 +25,10 @@ Overview
2325
--------
2426

2527
This section includes guides on how to use each of the available
26-
builders, and demonstrates the utility the MongoDB Java driver builder classes
28+
builders, and demonstrates the utility the MongoDB Kotlin driver builder classes
2729
provide.
2830

29-
The Java driver provides classes to simplify the process for developers
31+
The Kotlin driver provides classes to simplify the process for developers
3032
to use CRUD operations and the Aggregation API. The static utility methods allow you
3133
to build a query more efficiently.
3234

@@ -35,11 +37,11 @@ Why Use Builders?
3537

3638
Using the builders class, you leverage the power of:
3739

38-
- The Java compiler and the IDE to find errors during development
40+
- The Kotlin compiler and the IDE to find errors during development
3941
- The IDE for discovery and code completion
4042

41-
When using builders, the Java compiler and the IDE catch errors such as misspelled
42-
operators early on. When using the MongoDB shell or plain Java, you
43+
When using builders, the Kotlin compiler and the IDE catch errors such as misspelled
44+
operators early on. When using the MongoDB shell or plain Kotlin, you
4345
write operators as strings and get no visual indication of a problem,
4446
pushing these errors to runtime instead of compile time
4547

@@ -61,6 +63,12 @@ collection with the following criteria:
6163
We only want their email address, so we'll ensure our query doesn't
6264
return data we pay bandwidth costs for but don't need.
6365

66+
The documents in the ``users`` collection are modeled with the following data class
67+
in our application:
68+
69+
.. literalinclude:: /examples/generated/BuildersTest.snippet.user-data-class.kt
70+
:language: kotlin
71+
6472
Using the MongoDB Shell
6573
~~~~~~~~~~~~~~~~~~~~~~~
6674

@@ -71,32 +79,29 @@ Using the MongoDB Shell
7179
Without Using Builders
7280
~~~~~~~~~~~~~~~~~~~~~~
7381

74-
.. code-block:: java
75-
76-
Bson filter = new Document().append("gender", "female").append("age", new Document().append("$gt", 29));
77-
Bson projection = new Document().append("_id", 0).append("email", 1);
78-
collection.find(filter).projection(projection);
82+
.. literalinclude:: /examples/generated/BuildersTest.snippet.no-builders.kt
83+
:language: kotlin
7984

8085
Using Builders
8186
~~~~~~~~~~~~~~
8287

83-
.. code-block:: java
88+
.. code-block:: kotlin
8489

85-
import static com.mongodb.client.model.Filters.*;
86-
import static com.mongodb.client.model.Projections.*;
87-
...
90+
import com.mongodb.client.model.Filters.*
91+
import com.mongodb.client.model.Projections.*
8892

89-
Bson filter = and(eq("gender", "female"), gt("age", 29));
90-
Bson projection = fields(excludeId(), include("email"));
91-
collection.find(filter).projection(projection);
93+
.. literalinclude:: /examples/generated/BuildersTest.snippet.builders.kt
94+
:language: kotlin
9295

9396
Available Builders
9497
------------------
9598

96-
- :ref:`Filters <filters-builders>` for building query filters.
97-
- :ref:`Projections <projections-builders>` for building projections.
98-
- :ref:`Sorts <sorts-builders>` for building sort criteria.
99-
- :ref:`Updates <updates-builders>` for building updates.
10099
- :ref:`Aggregates <aggregates-builders>` for building aggregation pipelines.
101-
- :ref:`Indexes <indexes-builders>` for creating index keys.
100+
101+
.. TODO: add these back after Must-Have phase complete
102+
.. - :ref:`Filters <filters-builders>` for building query filters.
103+
.. - :ref:`Projections <projections-builders>` for building projections.
104+
.. - :ref:`Sorts <sorts-builders>` for building sort criteria.
105+
.. - :ref:`Updates <updates-builders>` for building updates.
106+
.. - :ref:`Indexes <indexes-builders>` for creating index keys.
102107

0 commit comments

Comments
 (0)