PyMongo,处理具有相对于数据库时间的时间的字段

PyMongo, handling fields with time relative to the database time

假设我有一个文档字段名称"creation_time"。我想用 数据库当前时间 初始化它的值,而不是使用客户端机器时间:

datetime.datetime.utcnow()

如何使用 PyMongo 实现此目的?

另外有什么方法可以在字段初始化的时候进行操作,比如:

"creation_time": magic_get_mongodb_time() + 10 小时

附带说明一下,我可以处理 unix 时间戳,不需要日期时间格式。

编辑:尚无法对日期进行操作(2015 年 5 月):https://jira.mongodb.org/browse/SERVER-3174

您可以使用 MongoDB 的 $currentdate operator in your update, this sets the value of a field to the current date, either as a Date or a timestamp。默认类型是日期。

db = con['test']            
test = db.test
test.update({}, { 
    '$currentDate': {
        'creation_time': { '$type': 'timestamp' }
    }
},upsert=True)