|
| 1 | +import com.mongodb.client.model.* |
| 2 | +import com.mongodb.client.model.Filters.* |
| 3 | +import com.mongodb.kotlin.client.MongoClient |
| 4 | + |
| 5 | +// start-data-class |
| 6 | +data class Restaurant(val name: String, val borough: String, val cuisine: String) |
| 7 | +// end-data-class |
| 8 | + |
| 9 | +fun main() { |
| 10 | + val uri = "<connection string>" |
| 11 | + |
| 12 | + val mongoClient = MongoClient.create(uri) |
| 13 | + val database = mongoClient.getDatabase("sample_restaurants") |
| 14 | + val collection = database.getCollection<Restaurant>("restaurants") |
| 15 | + |
| 16 | + // start-bulk-insert-one |
| 17 | + val blueMoon = InsertOneModel(Restaurant("Blue Moon Grill", "Brooklyn", "American")) |
| 18 | + // end-bulk-insert-one |
| 19 | + |
| 20 | + // start-bulk-update-one |
| 21 | + val updateOneFilter = Filters.eq(Restaurant::name.name, "White Horse Tavern") |
| 22 | + val updateOneDoc = Updates.set(Restaurant::borough.name, "Queens") |
| 23 | + val tavernUpdate = UpdateOneModel<Restaurant>(updateOneFilter, updateOneDoc) |
| 24 | + // end-bulk-update-one |
| 25 | + |
| 26 | + // start-bulk-update-many |
| 27 | + val updateManyFilter = Filters.eq(Restaurant::name.name, "Wendy's") |
| 28 | + val updateManyDoc = Updates.set(Restaurant::cuisine.name, "Fast food") |
| 29 | + val wendysUpdate = UpdateManyModel<Restaurant>(updateManyFilter, updateManyDoc) |
| 30 | + // end-bulk-update-many |
| 31 | + |
| 32 | + // start-bulk-replace-one |
| 33 | + val replaceFilter = Filters.eq(Restaurant::name.name, "Cooper Town Diner") |
| 34 | + val replaceDoc = Restaurant("Smith Town Diner", "Brooklyn", "American") |
| 35 | + val replacement = ReplaceOneModel(replaceFilter, replaceDoc) |
| 36 | + // end-bulk-replace-one |
| 37 | + |
| 38 | + // start-bulk-delete-one |
| 39 | + val deleteOne = DeleteOneModel<Restaurant>(Filters.eq( |
| 40 | + Restaurant::name.name, |
| 41 | + "Morris Park Bake Shop" |
| 42 | + )) |
| 43 | + // end-bulk-delete-one |
| 44 | + |
| 45 | + // start-bulk-delete-many |
| 46 | + val deleteMany = DeleteManyModel<Restaurant>(Filters.eq( |
| 47 | + Restaurant::cuisine.name, |
| 48 | + "Experimental" |
| 49 | + )) |
| 50 | + // end-bulk-delete-many |
| 51 | + |
| 52 | + // start-bulk-write-mixed |
| 53 | + val insertOneMdl = InsertOneModel(Restaurant("Red's Pizza", "Brooklyn", "Pizzeria")) |
| 54 | + val updateOneMdl = UpdateOneModel<Restaurant>( |
| 55 | + Filters.eq(Restaurant::name.name, "Moonlit Tavern"), |
| 56 | + Updates.set(Restaurant::borough.name, "Queens") |
| 57 | + ) |
| 58 | + val deleteManyMdl = DeleteManyModel<Restaurant>( |
| 59 | + Filters.eq(Restaurant::name.name, "Crepe") |
| 60 | + ) |
| 61 | + |
| 62 | + val bulkResult = collection.bulkWrite( |
| 63 | + listOf(insertOneMdl, updateOneMdl, deleteManyMdl) |
| 64 | + ) |
| 65 | + |
| 66 | + println(bulkResult) |
| 67 | + // end-bulk-write-mixed |
| 68 | + |
| 69 | + val bulkOperations = listOf(insertOneMdl, updateOneMdl, deleteManyMdl) |
| 70 | + |
| 71 | + // start-bulk-write-unordered |
| 72 | + val opts = BulkWriteOptions().ordered(false) |
| 73 | + collection.bulkWrite(bulkOperations, opts) |
| 74 | + // end-bulk-write-unordered |
| 75 | + |
| 76 | +} |
| 77 | + |
0 commit comments