Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - Select based on timestamp and update timestamp with zero

How do I select records from a date field which has a time (HH:MM:SS.Milisecond) value greater than zero from a MongoDB collection and update it with a time (HH:MM:SS) value as zero by keeping the date value the same as the existing value in a Python script?

The current data would look like as below -

1) "createdDate" : ISODate("2015-10-10T00:00:00Z")
2) "createdDate" : ISODate("2015-10-11T00:00:00Z")
3) "createdDate" : ISODate("2015-10-12T00:00:00Z")
4) "createdDate" : ISODate("2015-10-13T01:04:30.515Z")
5) "createdDate" : ISODate("2015-10-14T02:05:50.516Z")
6) "createdDate" : ISODate("2015-10-15T03:06:60.517Z")
7) "createdDate" : ISODate("2015-10-16T04:07:80.518Z")

How do I select only rows 4, 5, 6, and 7 using mongodbsql and update it with the timestamp as zero in a Python script?

After an update, the data would look like as below -

1) "createdDate" : ISODate("2015-10-10T00:00:00Z")
2) "createdDate" : ISODate("2015-10-11T00:00:00Z")
3) "createdDate" : ISODate("2015-10-12T00:00:00Z")
4) "createdDate" : ISODate("2015-10-13T00:00:00Z")
5) "createdDate" : ISODate("2015-10-14T00:00:00Z")
6) "createdDate" : ISODate("2015-10-15T00:00:00Z")
7) "createdDate" : ISODate("2015-10-16T00:00:00Z")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ISODate() is represented as a datetime object by PyMongo. MongoDB assumes that dates and times are in UTC. There are several ways to get midnight (start of a day) for a given UTC time d:

>>> from datetime import datetime, time, timedelta
>>> d = datetime(2015, 10, 13, 1, 4, 30, 515000)
>>> datetime(d.year, d.month, d.day) # @user3100115' answer
datetime.datetime(2015, 10, 13, 0, 0)   # 369 ns
>>> datetime.fromordinal(d.toordinal()) # 451 ns
datetime.datetime(2015, 10, 13, 0, 0)
>>> datetime.combine(d, time.min)       # 609 ns
datetime.datetime(2015, 10, 13, 0, 0)
>>> d - (d - d.min) % timedelta(days=1) # Python 3
datetime.datetime(2015, 10, 13, 0, 0)   # 1.87 μs
>>> datetime(*d.timetuple()[:3])
datetime.datetime(2015, 10, 13, 0, 0)   # 2.34 μs
>>> from calendar import timegm
>>> datetime.utcfromtimestamp((timegm(d.timetuple()) // 86400) * 86400) # POSIX
datetime.datetime(2015, 10, 13, 0, 0)   # 4.72 μs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...