This page shows the driver syntax for several MongoDB commands and links to their related reference and API documentation.
The examples on the page use the following data class to represent MongoDB documents:
data class Movie( val title: String, val year: Int, val rated: String? = "Not Rated", val genres: List<String>? = listOf())
Command
Syntax
Find a Document
API Documentation Usage Example Fundamentalscollection.find( Filters.eq(Movie::title.name, "Shrek")).firstOrNull()
Movie(title=Shrek, year=2001, ...)
Find Multiple Documents
API Documentation Usage Example Fundamentalscollection.find( Filters.eq(Movie::year.name, 2004))
[ Movie(title=Shrek 2, year=2004, ...), Movie(title=Spider-Man 2, year=2004, ...), Movie(title=National Treasure, year=2004, ...), ...]
Insert a Document
API Documentation Usage Example Fundamentalscollection.insertOne(Movie("Shrek", 2001))
Insert Multiple Documents
API Documentation Usage Example Fundamentalscollection.insertMany( listOf( Movie("Shrek", 2001), Movie("Shrek 2", 2004), Movie("Shrek the Third", 2007), Movie("Shrek Forever After", 2010), ))
Update a Document
API Documentation Usage Example Fundamentalscollection.updateOne( Filters.eq(Movie::title.name, "Shrek"), Updates.set(Movie::rated.name, "PG"))
Movie(title=Shrek, year=2001, rated=PG, genres=[])
Update Multiple Documents
API Documentation Usage Example Fundamentalscollection.updateMany( Filters.regex(Movie::title.name, "Shrek"), Updates.set(Movie::rated.name, "PG"))
[ Movie(title=Shrek, year=2001, rated=PG, genres=[]), Movie(title=Shrek 2, year=2004, rated=PG, genres=[]), Movie(title=Shrek the Third, year=2007, rated=PG, genres=[]), Movie(title=Shrek Forever After, year=2010, rated=PG, genres=[])]
Update an Array in a Document
API Documentation Fundamentalscollection.updateOne( Filters.eq(Movie::title.name, "Shrek"), Updates.addEachToSet(Movie::genres.name, listOf("Family", "Fantasy")))
Movie(title=Shrek, year=2001, rated=Not Rated, genres=[Family, Fantasy])
Replace a Document
API Documentation Usage Example Fundamentalscollection.replaceOne( Filters.eq(Movie::title.name, "Shrek"), Movie("Kersh", 1002, "GP"))
Movie(title=Kersh, year=1002, rated=GP, genres=[])
Delete a Document
API Documentation Usage Example Fundamentalscollection.deleteOne( Filters.eq(Movie::title.name, "Shrek"))
Delete Multiple Documents
API Documentation Usage Example Fundamentalscollection.deleteMany( Filters.regex(Movie::title.name, "Shrek"))
Bulk Write
API Documentation Usage Example Fundamentalscollection.bulkWrite( listOf( InsertOneModel(Movie("Shrek", 2001)), DeleteManyModel(Filters.lt(Movie::year.name, 2004)), ))
Watch for Changes
API Documentation Usage Example Fundamentalsval changeStream = collection.watch()changeStream.collect { println("Change to ${it.fullDocument?.title}")}
Access Results from a Query as a List
collection.find().toList()
[ Movie(title=Shrek, year=2001, rated=Not Rated, genres=[]), Movie(title=Shrek 2, year=2004, rated=Not Rated, genres=[]), Movie(title=Shrek the Third, year=2007, rated=Not Rated, genres=[]), Movie(title=Shrek Forever After, year=2010, rated=Not Rated, genres=[])]
Count Documents
API Documentation Usage Examplecollection.countDocuments(Filters.eq("year", 2001))
42
List the Distinct Documents or Field Values
API Documentation Usage Examplecollection.distinct<String>(Movie::rated.name)
[Not Rated, PG, PG-13]
Limit the Number of Documents Retrieved
API Documentation Fundamentalscollection.find() .limit(2)
[ Movie(title=Shrek, year=2001, rated=Not Rated, genres=[]), Movie(title=Shrek 2, year=2004, rated=Not Rated, genres=[])]
Skip Retrieved Documents
API Documentation Fundamentalscollection.find() .skip(2)
[ Movie(title=Shrek the Third, year=2007, rated=Not Rated, genres=[]), Movie(title=Shrek Forever After, year=2010, rated=Not Rated, genres=[])]
Sort the Documents When Retrieving Them
API Documentation Fundamentalscollection.find().sort(Sorts.descending(Movie::year.name))
[ Movie(title=Shrek Forever After, year=2010, rated=Not Rated, genres=[]), Movie(title=Shrek the Third, year=2007, rated=Not Rated, genres=[]), Movie(title=Shrek 2, year=2004, rated=Not Rated, genres=[]), Movie(title=Shrek, year=2001, rated=Not Rated, genres=[])]
Project Document Fields When Retrieving Them
API Documentation Usage Example Fundamentalsdata class Result(val title: String) collection.find<Result>() .projection(Projections.include(Movie::title.name))
Result(title=Shrek)
Create an Index
API Documentation Fundamentalscollection.createIndex(Indexes.ascending(Movie::title.name))
Search Text
API Documentation Fundamentalscollection.find(Filters.text("Forever"));
[Movie(title=Shrek Forever After, year=2010, rated=Not Rated, genres=[])]
Install the Driver Dependency with Maven
pom.xml
<dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-kotlin-coroutine</artifactId> </dependency></dependencies>
Install the Driver Dependency with Gradle
build.gradle.kts
dependencies { implementation("org.mongodb:mongodb-driver-kotlin-coroutine")}
Access Data from a Flow Iteratively
API Documentation Fundamentalsval flow = collection.find( Filters.eq(Movie::year.name, 2004))flow.collect { println(it) }
Movie(title=2001: A Space Odyssey, ...)Movie(title=The Sound of Music, ...)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4