Skip to content

Commit f2c2e68

Browse files
mongodbencbullingerrustagir
authored
Usage Examples Feature Branch (#59)
# Pull Request Info Feature branch to aggregate usage examples before merging into `master`. [PR Reviewing Guidelines](https://github.com/mongodb/docs-java/blob/master/REVIEWING.md) JIRA - N/A Staging - https://docs-mongodbcom-staging.corp.mongodb.com/kotlin/docsworker-xlarge/usage_examples/ ## 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: cbullinger <[email protected]> Co-authored-by: Rea Rustagi <[email protected]>
1 parent ea462a0 commit f2c2e68

File tree

65 files changed

+1862
-465
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1862
-465
lines changed

examples/src/main/kotlin/Main.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package usageExamples.command
2+
// :replace-start: {
3+
// "terms": {
4+
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string uri>\"",
5+
// "import io.github.cdimascio.dotenv.dotenv\n": ""
6+
// }
7+
// }
8+
// :snippet-start: command-usage-example
9+
10+
import com.mongodb.MongoException
11+
import com.mongodb.kotlin.client.coroutine.MongoClient
12+
import io.github.cdimascio.dotenv.dotenv
13+
import kotlinx.coroutines.runBlocking
14+
import org.bson.BsonDocument
15+
import org.bson.BsonInt64
16+
import org.bson.json.JsonWriterSettings
17+
18+
fun main() = runBlocking {
19+
// :remove-start:
20+
val dotenv = dotenv()
21+
val CONNECTION_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
22+
// :remove-end:
23+
// Replace the uri string with your MongoDB deployment's connection string
24+
val uri = CONNECTION_URI_PLACEHOLDER
25+
val mongoClient = MongoClient.create(uri)
26+
val database = mongoClient.getDatabase("sample_mflix")
27+
try {
28+
val command = BsonDocument("dbStats", BsonInt64(1))
29+
val commandResult = database.runCommand(command)
30+
println(commandResult.toJson(JsonWriterSettings.builder().indent(true).build()))
31+
} catch (me: MongoException) {
32+
System.err.println("An error occurred: $me")
33+
}
34+
mongoClient.close()
35+
}
36+
// :snippet-end:
37+
// :replace-end:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
👾
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package usageExamples.bulkWrite
2+
3+
// :replace-start: {
4+
// "terms": {
5+
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string uri>\"",
6+
// "import io.github.cdimascio.dotenv.dotenv\n": ""
7+
// }
8+
// }
9+
// :snippet-start: bulk-write-usage-example
10+
import com.mongodb.MongoException
11+
import com.mongodb.client.model.DeleteOneModel
12+
import com.mongodb.client.model.Filters
13+
import com.mongodb.client.model.InsertOneModel
14+
import com.mongodb.client.model.ReplaceOneModel
15+
import com.mongodb.client.model.UpdateOneModel
16+
import com.mongodb.client.model.UpdateOptions
17+
import com.mongodb.client.model.Updates
18+
import com.mongodb.kotlin.client.coroutine.MongoClient
19+
import io.github.cdimascio.dotenv.dotenv
20+
import kotlinx.coroutines.runBlocking
21+
22+
data class Movie(val title: String, val runtime: Int? = null)
23+
24+
fun main() = runBlocking {
25+
// :remove-start:
26+
val dotenv = dotenv()
27+
val CONNECTION_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
28+
// :remove-end:
29+
// Replace the uri string with your MongoDB deployment's connection string
30+
val uri = CONNECTION_URI_PLACEHOLDER
31+
val mongoClient = MongoClient.create(uri)
32+
val database = mongoClient.getDatabase("sample_mflix")
33+
val collection = database.getCollection<Movie>("movies")
34+
35+
try {
36+
val result = collection.bulkWrite(
37+
listOf(
38+
InsertOneModel(Movie("A Sample Movie")),
39+
InsertOneModel(Movie("Another Sample Movie")),
40+
InsertOneModel(Movie("Yet Another Sample Movie")),
41+
UpdateOneModel(
42+
Filters.eq(Movie::title.name,"A Sample Movie"),
43+
Updates.set(Movie::title.name, "An Old Sample Movie"),
44+
UpdateOptions().upsert(true)
45+
),
46+
DeleteOneModel(Filters.eq("title", "Another Sample Movie")),
47+
ReplaceOneModel(
48+
Filters.eq(Movie::title.name, "Yet Another Sample Movie"),
49+
Movie("The Other Sample Movie", 42)
50+
)
51+
)
52+
)
53+
println(
54+
"""
55+
Result statistics:
56+
inserted: ${result.insertedCount}
57+
updated: ${result.modifiedCount}
58+
deleted: ${result.deletedCount}
59+
""".trimIndent()
60+
)
61+
} catch (e: MongoException) {
62+
System.err.println("The bulk write operation failed due to an error: $e")
63+
}
64+
// :remove-start:
65+
// clean up
66+
database.drop()
67+
// :remove-end:
68+
mongoClient.close()
69+
}
70+
// :snippet-end:
71+
// :replace-end:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package usageExamples.count
2+
// :replace-start: {
3+
// "terms": {
4+
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string uri>\"",
5+
// "import io.github.cdimascio.dotenv.dotenv\n": ""
6+
// }
7+
// }
8+
// :snippet-start: count-usage-example
9+
10+
import com.mongodb.MongoException
11+
import com.mongodb.client.model.Filters
12+
import com.mongodb.kotlin.client.coroutine.MongoClient
13+
import io.github.cdimascio.dotenv.dotenv
14+
import kotlinx.coroutines.runBlocking
15+
16+
17+
data class Movie(val countries: List<String>)
18+
19+
fun main() = runBlocking {
20+
// :remove-start:
21+
val dotenv = dotenv()
22+
val CONNECTION_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
23+
// :remove-end:
24+
// Replace the uri string with your MongoDB deployment's connection string
25+
val uri = CONNECTION_URI_PLACEHOLDER
26+
val mongoClient = MongoClient.create(uri)
27+
val database = mongoClient.getDatabase("sample_mflix")
28+
val collection = database.getCollection<Movie>("movies")
29+
30+
31+
collection.insertOne(Movie(listOf("Spain", "USA"))) // :remove:
32+
val query = Filters.eq(Movie::countries.name, "Spain")
33+
try {
34+
val estimatedCount = collection.estimatedDocumentCount()
35+
println("Estimated number of documents in the movies collection: $estimatedCount")
36+
val matchingCount = collection.countDocuments(query)
37+
println("Number of movies from Spain: $matchingCount")
38+
} catch (e: MongoException) {
39+
System.err.println("An error occurred: $e")
40+
}
41+
42+
// :remove-start:
43+
// clean up
44+
database.drop()
45+
// :remove-end:
46+
mongoClient.close()
47+
}
48+
// :snippet-end:
49+
// :replace-end:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package usageExamples.delete
2+
3+
// :replace-start: {
4+
// "terms": {
5+
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string uri>\"",
6+
// "import io.github.cdimascio.dotenv.dotenv\n": ""
7+
// }
8+
// }
9+
// :snippet-start: delete-usage-example
10+
11+
import com.mongodb.MongoException
12+
import com.mongodb.client.model.Filters
13+
import com.mongodb.kotlin.client.coroutine.MongoClient
14+
import io.github.cdimascio.dotenv.dotenv
15+
import kotlinx.coroutines.runBlocking
16+
17+
18+
data class Movie(val title: String)
19+
20+
fun main() = runBlocking {
21+
// :remove-start:
22+
val dotenv = dotenv()
23+
val CONNECTION_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
24+
// :remove-end:
25+
// Replace the uri string with your MongoDB deployment's connection string
26+
val uri = CONNECTION_URI_PLACEHOLDER
27+
val mongoClient = MongoClient.create(uri)
28+
val database = mongoClient.getDatabase("sample_mflix")
29+
val collection = database.getCollection<Movie>("movies")
30+
31+
collection.insertOne(Movie("The Garbage Pail Kids Movie")) // :remove:
32+
val query = Filters.eq(Movie::title.name, "The Garbage Pail Kids Movie")
33+
34+
try {
35+
val result = collection.deleteOne(query)
36+
println("Deleted document count: " + result.deletedCount)
37+
} catch (e: MongoException) {
38+
System.err.println("Unable to delete due to an error: $e")
39+
}
40+
// :remove-start:
41+
// clean up
42+
database.drop()
43+
// :remove-end:
44+
mongoClient.close()
45+
}
46+
// :snippet-end:
47+
// :replace-end:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package usageExamples.deleteMany
2+
3+
// :replace-start: {
4+
// "terms": {
5+
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string uri>\"",
6+
// "import io.github.cdimascio.dotenv.dotenv\n": ""
7+
// }
8+
// }
9+
// :snippet-start: delete-many-usage-example
10+
11+
import com.mongodb.MongoException
12+
import com.mongodb.client.model.Filters
13+
import com.mongodb.kotlin.client.coroutine.MongoClient
14+
import io.github.cdimascio.dotenv.dotenv
15+
import kotlinx.coroutines.runBlocking
16+
17+
data class Movie(val imdb: IMDB){
18+
data class IMDB(val rating: Double)
19+
}
20+
21+
fun main() = runBlocking {
22+
// :remove-start:
23+
val dotenv = dotenv()
24+
val CONNECTION_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
25+
// :remove-end:
26+
// Replace the uri string with your MongoDB deployment's connection string
27+
val uri = CONNECTION_URI_PLACEHOLDER
28+
val mongoClient = MongoClient.create(uri)
29+
val database = mongoClient.getDatabase("sample_mflix")
30+
val collection = database.getCollection<Movie>("movies")
31+
32+
collection.insertMany(listOf(Movie(Movie.IMDB(rating = 2.1)), Movie(Movie.IMDB(rating = 2.5)), Movie(Movie.IMDB(rating = 1.9)), Movie(Movie.IMDB(rating = 1.5)))) // :remove:
33+
val query = Filters.lt("${Movie::imdb.name}.${Movie.IMDB::rating.name}", 2.9)
34+
try {
35+
val result = collection.deleteMany(query)
36+
println("Deleted document count: " + result.deletedCount)
37+
} catch (e: MongoException) {
38+
System.err.println("Unable to delete due to an error: $e")
39+
}
40+
// :remove-start:
41+
// clean up
42+
database.drop()
43+
// :remove-end:
44+
mongoClient.close()
45+
}
46+
// :snippet-end:
47+
// :replace-end:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package usageExamples.distinct
2+
3+
// :replace-start: {
4+
// "terms": {
5+
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string uri>\"",
6+
// "import io.github.cdimascio.dotenv.dotenv\n": ""
7+
// }
8+
// }
9+
// :snippet-start: distinct-usage-example
10+
import com.mongodb.MongoException
11+
import com.mongodb.client.model.Filters
12+
import com.mongodb.kotlin.client.coroutine.MongoClient
13+
import io.github.cdimascio.dotenv.dotenv
14+
import kotlinx.coroutines.runBlocking
15+
16+
data class Movie(val year: Int, val directors: List<String>)
17+
18+
fun main() = runBlocking {
19+
// :remove-start:
20+
val dotenv = dotenv()
21+
val CONNECTION_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
22+
// :remove-end:
23+
// Replace the uri string with your MongoDB deployment's connection string
24+
val uri = CONNECTION_URI_PLACEHOLDER
25+
val mongoClient = MongoClient.create(uri)
26+
val database = mongoClient.getDatabase("sample_mflix")
27+
val collection = database.getCollection<Movie>("movies")
28+
29+
collection.insertMany(listOf(Movie(1992,listOf("Carl Franklin")), Movie(1995,listOf("Carl Franklin")), Movie(1998,listOf("Carl Franklin"))))// :remove:
30+
try {
31+
val resultsFlow = collection.distinct<Int>(
32+
Movie::year.name, Filters.eq(Movie::directors.name, "Carl Franklin")
33+
)
34+
resultsFlow.collect { println(it) }
35+
} catch (e: MongoException) {
36+
System.err.println("An error occurred: $e")
37+
}
38+
39+
// :remove-start:
40+
// clean up
41+
database.drop()
42+
// :remove-end:
43+
mongoClient.close()
44+
}
45+
// :snippet-end:
46+
// :replace-end:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package usageExamples.find
2+
3+
// :replace-start: {
4+
// "terms": {
5+
// "CONNECTION_URI_PLACEHOLDER": "\"<connection string uri>\"",
6+
// "import io.github.cdimascio.dotenv.dotenv\n": ""
7+
// }
8+
// }
9+
// :snippet-start: find-usage-example
10+
import com.mongodb.client.model.Filters.lt
11+
import com.mongodb.client.model.Projections
12+
import com.mongodb.client.model.Sorts
13+
import com.mongodb.kotlin.client.coroutine.MongoClient
14+
import io.github.cdimascio.dotenv.dotenv
15+
import kotlinx.coroutines.runBlocking
16+
17+
data class Movie(val title: String, val runtime: Int, val imdb: IMDB){
18+
data class IMDB(val rating: Double)
19+
}
20+
21+
data class Results(val title: String)
22+
23+
fun main() = runBlocking {
24+
// :remove-start:
25+
val dotenv = dotenv()
26+
val CONNECTION_URI_PLACEHOLDER = dotenv["MONGODB_CONNECTION_URI"]
27+
// :remove-end:
28+
// Replace the uri string with your MongoDB deployment's connection string
29+
val uri = CONNECTION_URI_PLACEHOLDER
30+
val mongoClient = MongoClient.create(uri)
31+
val database = mongoClient.getDatabase("sample_mflix")
32+
val collection = database.getCollection<Movie>("movies")
33+
// :remove-start:
34+
val movies = listOf(Movie("A Movie", 10, Movie.IMDB(8.5)), Movie("Movie 2", 10, Movie.IMDB(6.9)), Movie("Long Movie", 55, Movie.IMDB(8.0)), Movie("Z Movie", 10, Movie.IMDB(8.6)))
35+
collection.insertMany(movies)
36+
// :remove-end:
37+
38+
val projectionFields= Projections.fields(
39+
Projections.include(Movie::title.name, Movie::imdb.name),
40+
Projections.excludeId()
41+
)
42+
val resultsFlow = collection.withDocumentClass<Results>()
43+
.find(lt(Movie::runtime.name, 15))
44+
.projection(projectionFields)
45+
.sort(Sorts.descending(Movie::title.name))
46+
47+
resultsFlow.collect { println(it) }
48+
49+
// :remove-start:
50+
// clean up
51+
database.drop()
52+
// :remove-end:
53+
mongoClient.close()
54+
}
55+
// :snippet-end:
56+
// :replace-end:

0 commit comments

Comments
 (0)