【发布时间】:2019-07-26 00:51:15
【问题描述】:
我需要将timestamp_ms 的类型从string 更改为double 并创建基于这个新timestamp_ms 字段的FixedDate 字段。然后,将时间戳信息更改为NewDate 字段中的 ISO 日期。
我使用了这个代码:
collection.update({
"FixedDate": {"$exists": False}
},[{
"$set":
{"FixedDate":
{"$convert":
{"input": "$timestamp_ms",
"to": "double"
}
}
}
},
{"$set":
{"NewDate":
{"$toDate": "$FixedDate"
}
}
}
], multi=True)
它给出了这个错误信息:
TypeError: document must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping
我的数据如下所示:
{'_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'}
【问题讨论】:
标签: python mongodb aggregate pymongo updates