|
| 1 | + |
| 2 | +import com.mongodb.client.model.Filters |
| 3 | +import com.mongodb.client.result.InsertOneResult |
| 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.* |
| 9 | +import org.bson.json.JsonObject |
| 10 | +import org.bson.types.ObjectId |
| 11 | +import org.junit.jupiter.api.AfterAll |
| 12 | +import org.junit.jupiter.api.Assertions.* |
| 13 | +import org.junit.jupiter.api.TestInstance |
| 14 | +import java.time.LocalDate |
| 15 | +import java.time.ZoneId |
| 16 | +import java.util.* |
| 17 | +import kotlin.test.* |
| 18 | + |
| 19 | + |
| 20 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 21 | +internal class DocumentsTest { |
| 22 | + |
| 23 | + companion object { |
| 24 | + private val dotenv = dotenv() |
| 25 | + val mongoClient = MongoClient.create(dotenv["MONGODB_CONNECTION_URI"]) |
| 26 | + |
| 27 | + @AfterAll |
| 28 | + @JvmStatic |
| 29 | + fun afterAll() { |
| 30 | + runBlocking { |
| 31 | + mongoClient.close() |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun documentTest() = runBlocking { |
| 38 | + // :snippet-start: create-document |
| 39 | + val author = Document("_id", ObjectId()) |
| 40 | + .append("name", "Gabriel García Márquez") |
| 41 | + .append( |
| 42 | + "dateOfDeath", |
| 43 | + Date.from( |
| 44 | + LocalDate.of(2014, 4, 17) |
| 45 | + .atStartOfDay(ZoneId.systemDefault()).toInstant() |
| 46 | + ) |
| 47 | + ) |
| 48 | + .append( |
| 49 | + "novels", listOf( |
| 50 | + Document("title", "One Hundred Years of Solitude").append("yearPublished", 1967), |
| 51 | + Document("title", "Chronicle of a Death Foretold").append("yearPublished", 1981), |
| 52 | + Document("title", "Love in the Time of Cholera").append("yearPublished", 1985) |
| 53 | + ) |
| 54 | + ) |
| 55 | + // :snippet-end: |
| 56 | + |
| 57 | + assertNotNull(author) |
| 58 | + assertEquals("Gabriel García Márquez", author.getString("name")) |
| 59 | + |
| 60 | + // :snippet-start: insert-document |
| 61 | + // val mongoClient = <code to instantiate your client> |
| 62 | + |
| 63 | + val database = mongoClient.getDatabase("fundamentals_data") |
| 64 | + val collection = database.getCollection<Document>("authors") |
| 65 | + val result = collection.insertOne(author) |
| 66 | + // :snippet-end: |
| 67 | + |
| 68 | + assertNotNull(result) |
| 69 | + assertEquals(true, result.wasAcknowledged()) |
| 70 | + |
| 71 | + // :snippet-start: retrieve-document |
| 72 | + val doc = collection.find(Filters.eq("name", "Gabriel García Márquez")).firstOrNull() |
| 73 | + doc?.let { |
| 74 | + println("_id: ${it.getObjectId("_id")}, name: ${it.getString("name")}, dateOfDeath: ${it.getDate("dateOfDeath")}") |
| 75 | + |
| 76 | + it.getList("novels", Document::class.java).forEach { novel -> |
| 77 | + println("title: ${novel.getString("title")}, yearPublished: ${novel.getInteger("yearPublished")}") |
| 78 | + } |
| 79 | + } |
| 80 | + // :snippet-end: |
| 81 | + |
| 82 | + assertNotNull(doc) |
| 83 | + assertEquals("Gabriel García Márquez", doc
10000
?.getString("name")) |
| 84 | + assertEquals(3, doc?.getList("novels", Document::class.java)?.size) |
| 85 | + |
| 86 | + // clean up |
| 87 | + collection.drop() |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun bsonDocumentTest() = runBlocking { |
| 92 | + // :snippet-start: create-bson-document |
| 93 | + val author = BsonDocument() |
| 94 | + .append("_id", BsonObjectId()) |
| 95 | + .append("name", BsonString("Gabriel García Márquez")) |
| 96 | + .append( |
| 97 | + "dateOfDeath", |
| 98 | + BsonDateTime( |
| 99 | + LocalDate.of(2014, 4, 17) |
| 100 | + .atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli() |
| 101 | + ) |
| 102 | + ) |
| 103 | + .append( |
| 104 | + "novels", BsonArray( |
| 105 | + listOf( |
| 106 | + BsonDocument().append("title", BsonString("One Hundred Years of Solitude")) |
| 107 | + .append("yearPublished", BsonInt32(1967)), |
| 108 | + BsonDocument().append("title", BsonString("Chronicle of a Death Foretold")) |
| 109 | + .append("yearPublished", BsonInt32(1981)), |
| 110 | + BsonDocument().append("title", BsonString("Love in the Time of Cholera")) |
| 111 | + .append("yearPublished", BsonInt32(1985)) |
| 112 | + ) |
| 113 | + ) |
| 114 | + ) |
| 115 | + // :snippet-end: |
| 116 | + |
| 117 | + assertNotNull(author) |
| 118 | + assertEquals("Gabriel García Márquez", author.getString("name").value) |
| 119 | + assertEquals(3, author.getArray("novels").size) |
| 120 | + |
| 121 | + // :snippet-start: insert-bson-document |
| 122 | + // val mongoClient = <code to instantiate your client> |
| 123 | + |
| 124 | + val database = mongoClient.getDatabase("fundamentals_data") |
| 125 | + val collection = database.getCollection<BsonDocument>("authors") |
| 126 | + |
| 127 | + val result: InsertOneResult = collection.insertOne(author) |
| 128 | + // :snippet-end: |
| 129 | + |
| 130 | + assertNotNull(result) |
| 131 | + assertEquals(true, result.wasAcknowledged()) |
| 132 | + |
| 133 | + // :snippet-start: retrieve-bson-document |
| 134 | + // <MongoCollection setup code here> |
| 135 | + |
| 136 | + val doc = collection.find(Filters.eq("name", "Gabriel García Márquez")).firstOrNull() |
| 137 | + doc?.let { |
| 138 | + println("_id: ${it.getObjectId("_id").value}, name: ${it.getString("name").value}, dateOfDeath: ${Date(it.getDateTime("dateOfDeath").value)}") |
| 139 | + |
| 140 | + it.getArray("novels").forEach { novel -> |
| 141 | + val novelDocument = novel.asDocument() |
| 142 | + println("title: ${novelDocument.getString("title").value}, yearPublished: ${novelDocument.getInt32("yearPublished").value}") |
| 143 | + } |
| 144 | + } |
| 145 | + // :snippet-end: |
| 146 | + |
| 147 | + assertNotNull(doc) |
| 148 | + assertEquals("Gabriel García Márquez", doc?.getString("name")?.value) |
| 149 | + assertEquals(3, doc?.getArray("novels")?.size) |
| 150 | + |
| 151 | + // clean up |
| 152 | + collection.drop() |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + fun jsonObjectTest() = runBlocking { |
| 157 | + // :snippet-start: create-json-object |
| 158 | + val ejsonStr = """ |
| 159 | + {"_id": {"${"$"}oid": "6035210f35bd203721c3eab8"}, |
| 160 | + "name": "Gabriel García Márquez", |
| 161 | + "dateOfDeath": {"${"$"}date": "2014-04-17T04:00:00Z"}, |
| 162 | + "novels": [ |
| 163 | + {"title": "One Hundred Years of Solitude","yearPublished": 1967}, |
| 164 | + {"title": "Chronicle of a Death Foretold","yearPublished": 1981}, |
| 165 | + {"title": "Love in the Time of Cholera","yearPublished": 1985}]} |
| 166 | + """.trimIndent() |
| 167 | + |
| 168 | + val author = JsonObject(ejsonStr) |
| 169 | + // :snippet-end: |
| 170 | + |
| 171 | + assertNotNull(author) |
| 172 | + assertEquals("Gabriel García Márquez", author.toBsonDocument().getString("name")?.value) |
| 173 | + |
| 174 | + // :snippet-start: insert-json-object |
| 175 | + // val mongoClient = <code to instantiate your client>; |
| 176 | + |
| 177 | + val database = mongoClient.getDatabase("fundamentals_data") |
| 178 | + val collection= database.getCollection<JsonObject>("authors") |
| 179 | + |
| 180 | + val result = collection.insertOne(author) |
| 181 | + // :snippet-end: |
| 182 | + assertNotNull(result) |
| 183 | + assertEquals(true, result.wasAcknowledged()) |
| 184 | + |
| 185 | + // :snippet-start: retrieve-json-object |
| 186 | + // val mongoClient = <code to instantiate your client>; |
| 187 | + |
| 188 | + val query = JsonObject("{\"name\": \"Gabriel Garc\\u00eda M\\u00e1rquez\"}") |
| 189 | + val jsonResult = collection.find(query).firstOrNull() |
| 190 | + jsonResult?.let { |
| 191 | + println("query result in extended json format: " + jsonResult.json) |
| 192 | + } |
| 193 | + // :snippet-end: |
| 194 | + assertNotNull(jsonResult) |
| 195 | + assertEquals("Gabriel García Márquez", jsonResult?.toBsonDocument()?.getString("name")?.value) |
| 196 | + |
| 197 | + // clean up |
| 198 | + collection.drop() |
| 199 | + } |
| 200 | +} |
0 commit comments