【问题标题】:Pymongo convert timestamp to date as a new fieldPymongo 将时间戳转换为日期作为新字段
【发布时间】:2019-06-13 02:27:20
【问题描述】:

我的集合中有许多实体,我必须在集合中创建一个新的日期字段以用于将来的查询。

{'_id': ObjectId('5afea920d326051990a7f337'), 'created_at': 'Fri May 18 10:21:07 +0000 2018', 'timestamp_ms': '1526638867739'}
{'_id': ObjectId('5afea920d326051990a7f339'), 'created_at': 'Fri May 18 10:21:08 +0000 2018', 'timestamp_ms': '1526638868310'}
{'_id': ObjectId('5afea972d326051c5c05bc11'), 'created_at': 'Fri May 18 10:22:30 +0000 2018', 'timestamp_ms': '1526638950799'}
{'_id': ObjectId('5afea974d326051c5c05bc16'), 'created_at': 'Fri May 18 10:22:32 +0000 2018', 'timestamp_ms': '1526638952160'}
{'_id': ObjectId('5afea974d326051c5c05bc17'), 'created_at': 'Fri May 18 10:22:32 +0000 2018', 'timestamp_ms': '1526638952841'}

我需要将 timestamp_ms 转换成这样的日期格式:

{'_id': ObjectId('5afea920d326051990a7f337'), 'created_at': 'Fri May 18 10:21:07 +0000 2018', 'timestamp_ms': '1526638867739’, 'NewDate': '2018 05 18 10:21:07'}
{'_id': ObjectId('5afea920d326051990a7f339'), 'created_at': 'Fri May 18 10:21:08 +0000 2018', 'timestamp_ms': '1526638868310’, 'NewDate': '2018 05 18 10:21:08'}
{'_id': ObjectId('5afea972d326051c5c05bc11'), 'created_at': 'Fri May 18 10:22:30 +0000 2018', 'timestamp_ms': '1526638950799’, 'NewDate': '2018 05 18 10:22:30'}
{'_id': ObjectId('5afea974d326051c5c05bc16'), 'created_at': 'Fri May 18 10:22:32 +0000 2018', 'timestamp_ms': '1526638952160’, 'NewDate': '2018 05 18 10:22:32'}
{'_id': ObjectId('5afea974d326051c5c05bc17'), 'created_at': 'Fri May 18 10:22:32 +0000 2018', 'timestamp_ms': '1526638952841’, 'NewDate': '2018 05 18 10:22:32'}

我使用了这段代码(Python 3.6、pymongo 3.8、mongodb 4.0):

pipeline = [
    {
        '$addFields': {
            'newDate': {
                '$toDate': '$timestamp_ms'
            }
        }
    }
]
cursor = collection.aggregate(pipeline)

但是给出了这个错误信息:pymongo.errors.OperationFailure: Error parsing date string '1526638867739'; 12: Unexpected character '9'

我不确定聚合是完成这项任务的正确方法。 datetime.strptime() 对于'created_at' 可能会更好,但我还没有弄清楚如何将它实现到db.Mycollection_update_many()

【问题讨论】:

    标签: python mongodb date timestamp pymongo


    【解决方案1】:

    在 pymongo 和 mongodb 4.0 中使用以下查询

    db.test.aggregate(
        [
            {
                "$addFields": {
                    "NewDate": {"$toDate": "$timestamp_ms"} 
                }
            },
            {
                "$out": "test"
            },
        ],
    )
    

    【讨论】:

    • 亲爱的阿米特·博克。它给出了相同的错误代码。 pymongo.errors.OperationFailure:解析日期字符串“1526638867739”时出错; 12:意外字符“9”
    【解决方案2】:

    我在 MongoDB 大学论坛上从 Kanika Singla 那里得到了我的问题的答案。如果您有同样的问题,答案就在这里。

    pipeline = [
        {
            '$project': {
                'yearMonthDayUTC': {
                    '$convert': {
                        'to': 'double',
                        'input': '$timestamp_ms'
                    }
                }
            }
        }, {
            '$addFields': {
                'newDate': {
                    '$toDate': '$yearMonthDayUTC'
                }
            }
        }
    ]
    

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 2019-07-21
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多