【问题标题】:Convert MongoDB field from String to ISODate in array将 MongoDB 字段从字符串转换为数组中的 ISODate
【发布时间】:2019-08-08 07:38:02
【问题描述】:

我需要在 MongoDB 中将存储数组值的 type 从 String 更改为 ISODate。该过程应该更改存储的类型,不仅仅是以新格式将它们投影出来。

文档结构如下,目标值嵌套在absences[].date的数组中。

[{
    "id": 3086,
    "first": "Knox",
    "last": "Keith",
    "middle": "Kent",
    "absences": [{
            "date": "Wed Nov 28 2018 15:12:09 GMT+0000 (UTC)",
            "code": "X",
            "type": "E",
            "isPartial": false
        },
        {
            "date": "Wed Dec 26 2018 12:35:46 GMT+0000 (UTC)",
            "code": "X",
            "type": "E",
            "isPartial": false
        }
    ]
}]

我可以使用$set 轻松更改这些字段的值(但不能更改类型):

db.students.update(
   { },
   { $set: { "absences.$[].date" : "changed" } },
   { multi: true }
)

@JohnnyHK 分享了这个将字符串更改为 ISODate 的示例,但这仅适用于顶级对象(不适用于数组)。

db.snippets.find({created_at: {$not: {$type: 9}}}).forEach(function(doc) {
    doc.created_at = new Date(doc.created_at);
    db.snippets.save(doc);
})

我将不胜感激有关结合这两种策略的建议,即循环通过absences 数组将date 字段从String 转换为ISODate。

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    这可以使用下面的聚合管道来实现。

    db.students.aggregate([
        {
            '$addFields': {
                'absences': {
                    '$map': {
                        'input': '$absences', 
                        'as': 'absence', 
                        'in': {
                            'date': {
                                '$toDate': {
                                    '$substr': [
                                        '$$absence.date', 0, {
                                            '$subtract': [
                                                {
                                                    '$strLenCP': '$$absence.date'
                                                }, 5
                                            ]
                                        }
                                    ]
                                }
                            }, 
                            'code': '$$absence.code', 
                            'type': '$$absence.type', 
                            'isPartial': '$$absence.isPartial'
                        }
                    }
                }
            }
        }, {
            '$out': 'students'
        }
    ])
    

    【讨论】:

    • 据我了解,$addFields 仅以不同的格式投影数据。它不会改变底层的BSON value type。
    • 检查最后的$out 阶段。 $out 阶段,获取聚合管道返回的文档并将它们写入指定的集合。如果 $out 操作指定的集合已经存在,那么在聚合完成后,$out 阶段会自动将现有集合替换为新的结果集合。 docs.mongodb.com/manual/reference/operator/aggregation/out
    • 我已经修复了日期转换问题,查询应该对你有用。
    • 感谢您提供此解决方案,并让我了解 $out。
    • 虽然日期字段现在采用 ISODate() 格式,但 UTC 偏移量存在可变变化。注意下面的两个字段有一个 -0700 偏移量和一个 -0800 偏移量。您对为什么会发生这种情况有任何想法吗? {“日期”:ISODate(“2019-01-28T16:18:42.000-08:00”),“代码”:“X”,“类型”:“E”,“isPartial”:假},{“日期" : ISODate("2019-03-14T08:25:53.000-07:00"), "code" : "U", "type" : "U", "isPartial" : false }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多