文档菜单
文档首页
/ / /
Kotlin 协程

快速参考

本页面展示了多个 MongoDB 命令的驱动语法以及它们相关的参考和 API 文档链接。

页面上的示例使用了以下数据类来表示 MongoDB 文档

data class Movie(
val title: String,
val year: Int,
val rated: String? = "Not Rated",
val genres: List<String>? = listOf()
)
命令
语法
查找文档

API 文档
使用示例
collection.find(
Filters.eq(Movie::title.name, "Shrek")
).firstOrNull()
Movie(title=Shrek, year=2001, ...)
collection.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, ...),
...
]
collection.insertOne(Movie("Shrek", 2001))
collection.insertMany(
listOf(
Movie("Shrek", 2001),
Movie("Shrek 2", 2004),
Movie("Shrek the Third", 2007),
Movie("Shrek Forever After", 2010),
)
)
collection.updateOne(
Filters.eq(Movie::title.name, "Shrek"),
Updates.set(Movie::rated.name, "PG")
)
Movie(title=Shrek, year=2001, rated=PG, genres=[])
collection.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=[])
]
更新文档中的数组

collection.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])
collection.replaceOne(
Filters.eq(Movie::title.name, "Shrek"),
Movie("Kersh", 1002, "GP")
)
Movie(title=Kersh, year=1002, rated=GP, genres=[])
collection.deleteOne(
Filters.eq(Movie::title.name, "Shrek")
)
collection.deleteMany(
Filters.regex(Movie::title.name, "Shrek")
)
collection.bulkWrite(
listOf(
InsertOneModel(Movie("Shrek", 2001)),
DeleteManyModel(Filters.lt(Movie::year.name, 2004)),
)
)
val changeStream = collection.watch()
changeStream.collect {
println("Change to ${it.fullDocument?.title}")
}
将查询结果作为列表访问
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=[])
]
collection.countDocuments(Filters.eq("year", 2001))
42
列出不同的文档或字段值
collection.distinct<String>(Movie::rated.name)
[Not Rated, PG, PG-13]
限制检索的文档数量

collection.find()
.limit(2)
[
Movie(title=Shrek, year=2001, rated=Not Rated, genres=[]),
Movie(title=Shrek 2, year=2004, rated=Not Rated, genres=[])
]
跳过检索的文档

collection.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=[])
]
在检索时对文档进行排序

collection.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=[])
]
在检索时投影文档字段

data class Result(val title: String)
collection.find<Result>()
.projection(Projections.include(Movie::title.name))
Result(title=Shrek)
collection.createIndex(Indexes.ascending(Movie::title.name))
collection.find(Filters.text("Forever"));
[Movie(title=Shrek Forever After, year=2010, rated=Not Rated, genres=[])]
使用Maven安装驱动依赖
pom.xml
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-kotlin-coroutine</artifactId>
<version>5.2.1</version>
</dependency>
</dependencies>
使用Gradle安装驱动依赖
build.gradle.kts
dependencies {
implementation("org.mongodb:mongodb-driver-kotlin-coroutine:5.2.1")
}
迭代地从流中访问数据

val 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, ...)

返回

快速入门