我的集合中有许多实体,我必须在集合中创建一个新的日期字段,以便将来查询。在
{'_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'}
我需要将时间戳转换成如下日期格式:
^{pr2}$我使用了以下代码(Python3.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()
中。在
我在MongoDB大学论坛上得到了Kanika Singla的回答。答案在这里,如果你有同样的问题。在
在pymongo和mongodb 4.0中使用下面的查询
相关问题 更多 >
编程相关推荐