flask - 为什么mongodb插入数据经常出现时间相同的情况
高洛峰
高洛峰 2017-04-21 10:58:02
0
2
576

用 mongoengine 建立 collection,其中时间是这么定义的

from mongoengine import Document, DateTimeField
import datetime

class Post(Document):
    created = DateTimeField(default=datetime.datetime.utcnow())

在插入数据的过程中,老是出现插入时间相同的情况,下面这几条都是我手动插入的数据,但是发现有些十句的时间居然是一模一样的,下面是读出来的数据,可以看到好几条时间一样的,而这几条数据中间都间隔了好几分钟的:

50f2bf6c674d9a1136de4964 • 2013-01-13 14:06:33.717000 • root • 0

50f2bf9c674d9a1136de4965 • 2013-01-13 14:06:33.717000 • root • 0

50f2bfc6674d9a1136de4966 • 2013-01-13 14:06:33.717000 • root • 0

50f2c01f674d9a1136de4967 • 2013-01-13 14:06:33.717000 • admin • 0

50f2c1b8674d9a1136de4968 • 2013-01-13 14:06:33.717000 • admin • 0

50f2b909674d9a1118d57170 • 2013-01-13 13:37:03.176000 • root • 0

50f2b681674d9a11023af511 • 2013-01-13 13:28:07.676000 • root • 0
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
刘奇

There has been a similar problem on the Django platform. The Post date is only generated after the object is created, so the time will be the same
http://stackoverflow.com/questions/27...

MongoEngine is based on Django

Solution:

Replace datetime.datetime.utcnow() 改为 datetime.datetime.utcnow and make sure there are no brackets here

PHPzhong
default=datetime.datetime.utcnow()

It is estimated that datetime.datetime.utcnow() is executed here first

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template