Skip to content

Commit 100a23d

Browse files
mongodbenjyemin
andauthored
(DOCSP-29224): Data class document data type (#47)
# Pull Request Info [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - https://jira.mongodb.org/browse/DOCSP-29224 Staging - https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/DOCSP-29224/fundamentals/data-formats/document-data-format-data-class/ ## Self-Review Checklist - [ ] Is this free of any warnings or errors in the RST? - [ ] Did you run a spell-check? - [ ] Did you run a grammar-check? - [ ] Are all the links working? --------- Co-authored-by: Jeff Yemin <[email protected]>
1 parent 9f80820 commit 100a23d

11 files changed

+347
-1
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
2+
import com.mongodb.client.model.Filters
3+
import com.mongodb.kotlin.client.coroutine.MongoClient
4+
import io.github.cdimascio.dotenv.dotenv
5+
import kotlinx.coroutines.flow.count
6+
import kotlinx.coroutines.flow.firstOrNull
7+
import kotlinx.coroutines.runBlocking
8+
import org.bson.BsonType
9+
import org.bson.codecs.pojo.annotations.BsonId
10+
import org.bson.codecs.pojo.annotations.BsonProperty
11+
import org.bson.codecs.pojo.annotations.BsonRepresentation
12+
import org.bson.types.ObjectId
13+
import org.junit.jupiter.api.AfterAll
14+
import org.junit.jupiter.api.AfterEach
15+
import org.junit.jupiter.api.Assertions.*
16+
import org.junit.jupiter.api.TestInstance
17+
import java.util.*
18+
import kotlin.test.*
19+
20+
21+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
22+
internal class DataClassTest {
23+
24+
companion object {
25+
val dotenv = dotenv()
26+
val client = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"])
27+
val database = client.getDatabase("storage_store")
28+
29+
@AfterAll
30+
@JvmStatic
31+
fun afterAll() {
32+
runBlocking {
33+
client.close()
34+
}
35+
}
36+
}
37+
38+
@AfterEach
39+
fun afterEach() {
40+
runBlocking {
41+
database.drop()
42+
}
43+
}
44+
45+
// :snippet-start: data-class
46+
data class DataStorage(val productName: String, val capacity: Double)
47+
// :snippet-end:
48+
49+
@Test
50+
fun insertDataClassTest() = runBlocking {
51+
// :snippet-start: insert-data-class
52+
val collection = database.getCollection<DataStorage>("data_storage")
53+
val record = DataStorage("tape", 5.0)
54+
collection.insertOne(record)
55+
// :snippet-end:
56+
val result = collection.find().firstOrNull()
57+
assertEquals(record, result)
58+
}
59+
60+
@Test
61+
fun retrieveDataClassTest() = runBlocking {
62+
// :snippet-start: retrieve-data-class
63+
val collection = database.getCollection<DataStorage>("data_storage_devices")
64+
// :remove-start:
65+
val tape = DataStorage("tape", 5.0)
66+
collection.insertOne(tape)
67+
// :remove-end:
68+
69+
// Retrieve and print the documents as data classes
70+
val resultsFlow = collection.find()
71+
resultsFlow.collect { println(it) }
72+
// :snippet-end:
73+
assertEquals(tape, resultsFlow.firstOrNull())
74+
}
75+
76+
// :snippet-start: annotated-data-class
77+
data class NetworkDevice(
78+
@BsonId
79+
@BsonRepresentation(BsonType.OBJECT_ID)
80+
val deviceId: String,
81+
val name: String,
82+
@BsonProperty("type")
83+
val deviceType: String
84+
)
85+
// :snippet-end:
86+
87+
@Test
88+
fun insertAnnotatedDataClassTest() = runBlocking {
89+
// :snippet-start: insert-annotated-data-class
90+
val collection = database.getCollection<NetworkDevice>("network_devices")
91+
92+
// Insert the record
93+
val deviceId = ObjectId().toHexString()
94+
val device = NetworkDevice(deviceId, "Enterprise Wi-fi", "router")
95+
collection.insertOne(device)
96+
// :snippet-end:
97+
val result = collection.find().firstOrNull()
98+
assertEquals(device, result)
99+
}
100+
101+
@Test
102+
fun retrieveAnnotatedDataClassTest() = runBlocking {
103+
// :snippet-start: retrieve-annotated-data-class
104+
val collection = database.getCollection<NetworkDevice>("network_devices")
105+
// :remove-start:
106+
val deviceId = ObjectId().toHexString()
107+
val device = NetworkDevice(deviceId, "Enterprise Wi-fi", "router")
108+
collection.insertOne(device)
109+
// :remove-end:
110+
111+
// Return all documents in the collection as data classes
112+
val resultsFlow = collection.find()
113+
resultsFlow.collect { println(it) }
114+
// :snippet-end:
115+
assertEquals(device, resultsFlow.firstOrNull())
116+
assertEquals(1, resultsFlow.count())
117+
}
118+
119+
@Test
120+
fun recursiveDataClassTypesTest() = runBlocking {
121+
// :snippet-start: recursive-data-class-types
122+
data class DataClassTree(
123+
val content: String,
124+
val left: DataClassTree?,
125+
val right: DataClassTree?
126+
)
127+
// :snippet-end:
128+
// :snippet-start: recursive-data-class-types-retrieve
129+
val collection = database.getCollection<DataClassTree>("myCollection")
130+
// :remove-start:
131+
val exampleTree = DataClassTree(
132+
"indo-european",
133+
DataClassTree(
134+
"germanic",
135+
DataClassTree("german", null, DataClassTree("high german", null, null)),
136+
DataClassTree(
137+
"norse", DataClassTree("swedish", null, null),
138+
DataClassTree("danish", null, null)
139+
)
140+
),
141+
DataClassTree(
142+
"romance",
143+
DataClassTree("spanish", null, null),
144+
DataClassTree("french", null, null)
145+
)
146+
)
147+
collection.insertOne(exampleTree)
148+
// :remove-end:
149+
150+
val filter = Filters.eq("left.left.right.content", "high german")
151+
val resultsFlow = collection.find(filter)
152+
resultsFlow.collect { println(it) }
153+
// :snippet-end:
154+
assertEquals(exampleTree, resultsFlow.firstOrNull())
155+
}
156+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data class NetworkDevice(
2+
@BsonId
3+
@BsonRepresentation(BsonType.OBJECT_ID)
4+
val deviceId: String,
5+
val name: String,
6+
@BsonProperty("type")
7+
val deviceType: String
8+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data class DataStorage(val productName: String, val capacity: Double)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val collection = database.getCollection<NetworkDevice>("network_devices")
2+
3+
// Insert the record
4+
val deviceId = ObjectId().toHexString()
5+
val device = NetworkDevice(deviceId, "Enterprise Wi-fi", "router")
6+
collection.insertOne(device)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val collection = database.getCollection<DataStorage>("data_storage")
2+
val record = DataStorage("tape", 5.0)
3+
collection.insertOne(record)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val collection = database.getCollection<DataClassTree>("myCollection")
2+
3+
val filter = Filters.eq("left.left.right.content", "high german")
4+
val resultsFlow = collection.find(filter)
5+
resultsFlow.collect { println(it) }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data class DataClassTree(
2+
val content: String,
3+
val left: DataClassTree?,
4+
val right: DataClassTree?
5+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val collection = database.getCollection<NetworkDevice>("network_devices")
2+
3+
// Return all documents in the collection as data classes
4+
val resultsFlow = collection.find()
5+
resultsFlow.collect { println(it) }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val collection = database.getCollection<DataStorage>("data_storage_devices")
2+
3+
// Retrieve and print the documents as data classes
4+
val resultsFlow = collection.find()
5+
resultsFlow.collect { println(it) }

source/fundamentals/data-formats.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ Data Formats
44

55
.. default-domain:: mongodb
66

7+
- :doc:`/fundamentals/data-formats/document-data-format-data-class`
78
- :doc:`/fundamentals/data-formats/document-data-format-bson`
89
- :doc:`/fundamentals/data-formats/document-data-format-extended-json`
910
- :doc:`/fundamentals/data-formats/documents`
1011

1112
.. toctree::
1213
:caption: Data Formats
1314

15+
/fundamentals/data-formats/document-data-format-data-class
1416
/fundamentals/data-formats/document-data-format-bson
1517
/fundamentals/data-formats/document-data-format-extended-json
1618
/fundamentals/data-formats/documents

0 commit comments

Comments
 (0)