【发布时间】:2020-02-27 14:25:55
【问题描述】:
Mongo DB 允许在更新操作中使用聚合管道。 https://docs.mongodb.com/manual/tutorial/update-documents-with-aggregation-pipeline/
我想使用 Kotlin/Java 驱动程序来实现这一点。我目前的情况我想将文档中的日期属性更改为字符串。不确定是否可以这样做,我在 Java 驱动程序文档中没有看到任何文档(ofc 下面的代码不起作用,这只是一个草稿):
fun changeProductLaunchDate(db: MongoDatabase) {
db.getCollection("productLine").updateMany(
and(
ne("products", null),
or(
type("products.launchDate", BsonType.DATE_TIME),
)
),
set(
"products.launchDate", Document(
"$dateToString", mapOf(
"date" to "\$products.launchDate",
"format" to "%d-%m-%Y",
"timezone" to "GMT",
"onNull" to null
)
)
)
)
}
有人有这方面的经验吗?任何建议都会有所帮助。
【问题讨论】:
标签: java mongodb kotlin aggregation-framework