【问题标题】:Mongo DB updates with aggregation pipeline with Java Driver使用 Java 驱动程序使用聚合管道更新 Mongodb
【发布时间】: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


    【解决方案1】:

    成功了

            db.getCollection("groupProductLine").updateMany(
            and(
                ne("products", null),
                or(
                    type("products.launchDate", BsonType.DATE_TIME)
                )
            ),
            listOf(
                set(
                    "products.lLaunchDate",
                    Document(
                        "\$dateToString",
                        Document("date", "\$products.launchDate")
                            .append("format", "%d-%m-%Y")
                            .append("timezone", "+02:00")
                            .append("onNull", null)
                    )
                )
            )
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多