|
| 1 | +import com.mongodb.MongoException |
| 2 | +import com.mongodb.client.model.Filters.* |
| 3 | +import com.mongodb.kotlin.client.coroutine.MongoClient |
| 4 | +import kotlinx.coroutines.flow.firstOrNull |
| 5 | +import kotlinx.coroutines.runBlocking |
| 6 | +import org.bson.Document |
| 7 | + |
| 8 | +fun main() = runBlocking { |
| 9 | + val uri = "<connection string>" |
| 10 | + val mongoClient = MongoClient.create(uri) |
| 11 | + val database = mongoClient.getDatabase("db") |
| 12 | + val collection = database.getCollection<Document>("inventory") |
| 13 | + |
| 14 | + // Start Example 1 |
| 15 | + val result = collection.insertOne( |
| 16 | + Document("item", "canvas") |
| 17 | + .append("qty", 100) |
| 18 | + .append("tags", listOf("cotton")) |
| 19 | + .append("size", Document("h", 28) |
| 20 | + .append("w", 35.5) |
| 21 | + .append("uom", "cm") |
| 22 | + ) |
| 23 | + ) |
| 24 | + // End Example 1 |
| 25 | + |
| 26 | + println("Success! Inserted document: " + result.insertedId) |
| 27 | + |
| 28 | + // Start Example 2 |
| 29 | + val flowInsertOne = collection.withDocumentClass<Document>() |
| 30 | + .find(eq("item", "canvas")) |
| 31 | + .firstOrNull() |
| 32 | + // End Example 2 |
| 33 | + |
| 34 | + if (flowInsertOne == null) { |
| 35 | + println("No results found."); |
| 36 | + } else { |
| 37 | + println(flowInsertOne) |
| 38 | + } |
| 39 | + |
| 40 | + collection.deleteMany(empty()) |
| 41 | + |
| 42 | + // Start Example 3 |
| 43 | + val results = collection.insertMany( |
| 44 | + listOf( |
| 45 | + Document("item", "journal") |
| 46 | + .append("qty", 25) |
| 47 | + .append("tags", listOf("blank", "red")) |
| 48 | + .append("size", Document("h", 14) |
| 49 | + .append("w", 21) |
| 50 | + .append("uom", "cm") |
| 51 | + ), |
| 52 | + Document("item", "mat") |
| 53 | + .append("qty", 25) |
| 54 | + .append("tags", listOf("gray")) |
| 55 | + .append("size", Document("h", 27.9) |
| 56 | + .append("w", 35.5) |
| 57 | + .append("uom", "cm") |
| 58 | + ), |
| 59 | + Document("item", "mousepad") |
| 60 | + .append("qty", 25) |
| 61 | + .append("tags", listOf("gel", "blue")) |
| 62 | + .append("size", Document("h", 19) |
| 63 | + .append("w", 22.85) |
| 64 | + .append("uom", "cm") |
| 65 | + ) |
| 66 | + ) |
| 67 | + ) |
| 68 | + // End Example 3 |
| 69 | + |
| 70 | + println("Success! Inserted documents: " + results.insertedIds) |
| 71 | + |
| 72 | + // Start Example 7 |
| 73 | + val flowInsertMany = collection.withDocumentClass<Document>() |
| 74 | + .find(empty()) |
| 75 | + // End Example 7 |
| 76 | + |
| 77 | + flowInsertMany.collect { println(it) } |
| 78 | + collection.deleteMany(empty()) |
| 79 | + |
| 80 | + // Start Example 6 |
| 81 | + collection.insertMany( |
| 82 | + listOf( |
| 83 | + Document("item", "journal") |
| 84 | + .append("qty", 25) |
| 85 | + .append("size", Document("h", 14) |
| 86 | + .append("w", 21) |
| 87 | + .append("uom", "cm") |
| 88 | + ) |
| 89 | + .append("status", "A"), |
| 90 | + Document("item", "notebook") |
| 91 | + .append("qty", 50) |
| 92 | + .append("size", Document("h", 8.5) |
| 93 | + .append("w", 11) |
| 94 | + .append("uom", "in") |
| 95 | + ) |
| 96 | + .append("status", "A"), |
| 97 | + Document("item", "paper") |
| 98 | + .append("qty", 100) |
| 99 | + .append("size", Document("h", 8.5) |
| 100 | + .append("w", 11) |
| 101 | + .append("uom", "in") |
| 102 | + ) |
| 103 | + .append("status", "D"), |
| 104 | + Document("item", "planner") |
| 105 | + .append("qty", 75) |
| 106 | + .append("size", Document("h", 22.85) |
| 107 | + .append("w", 30) |
| 108 | + .append("uom", "cm") |
| 109 | + ) |
| 110 | + .append("status", "D"), |
| 111 | + Document("item", "postcard") |
| 112 | + .append("qty", 45) |
| 113 | + .append("size", Document("h", 10) |
| 114 | + .append("w", 15.25) |
| 115 | + .append("uom", "cm") |
| 116 | + ) |
| 117 | + .append("status", "A"), |
| 118 | + ) |
| 119 | + ) |
| 120 | + // End Example 6 |
| 121 | + |
| 122 | + // Start Example 9 |
| 123 | + val flowFindEquality = collection.withDocumentClass<Document>() |
| 124 | + .find(eq("status", "D")) |
| 125 | + // End Example 9 |
| 126 | + |
| 127 | + flowFindEquality.collect { println(it) } |
| 128 | + |
| 129 | + // Start Example 10 |
| 130 | + val flowFindOperator = collection.withDocumentClass<Document>() |
| 131 | + .find(`in`("status", "A", "D")) |
| 132 | + // End Example 10 |
| 133 | + |
| 134 | + flowFindOperator.collect { println(it) } |
| 135 | + |
| 136 | + // Start Example 11 |
| 137 | + val flowFindAND = collection.withDocumentClass<Document>() |
| 138 | + .find(and(eq("status", "A"), lt("qty", 30))) |
| 139 | + // End Example 11 |
| 140 | + |
| 141 | + flowFindAND.collect { println(it) } |
| 142 | + |
| 143 | + // Start Example 12 |
| 144 | + val flowFindOR = collection.withDocumentClass<Document>() |
| 145 | + .find(or(eq("status", "A"), lt("qty", 30))) |
| 146 | + // End Example 12 |
| 147 | + |
| 148 | + flowFindOR.collect { println(it) } |
| 149 | + |
| 150 | + // Start Example 13 |
| 151 | + val flowFindANDOR = collection.withDocumentClass<Document>() |
| 152 | + .find( |
| 153 | + and(eq("status", "A"), |
| 154 | + or(lt("qty", 30), regex("item", "^p"))) |
| 155 | + ) |
| 156 | + // End Example 13 |
| 157 | + |
| 158 | + flowFindANDOR.collect { println(it) } |
| 159 | + collection.deleteMany(empty()) |
| 160 | + |
| 161 | + mongoClient.close() |
| 162 | +} |
0 commit comments