select based on timestamp and update timestamp with zero
如何从MongoDB集合中时间(hh:mm:ss.milisond)值大于零的日期字段中选择记录,并使用时间(hh:mm:ss)值将其更新为零,方法是保持日期值与Python脚本中现有的值相同。
当前数据如下-
1 2 3 4 5 6 7 | 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") |
如何使用mongodbsql只选择行4、5、6、7,并在python脚本中使用timestamp作为零进行更新-
更新后的数据如下-
1 2 3 4 5 6 7 | 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") |
pymongo将
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | >>> 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 |
更新文档和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from datetime import datetime from pymongo import MongoClient client = MongoClient() db = client.test collection = db.collection bulkOp = collection.initialize_ordered_bulk_op() count = 0 for doc in collection.find(): year = doc['createdDate'].year month = doc['createdDate'].month day = doc['createdDate'].day new_date = datetime(year, month, day) bulkOp.find({'_id': doc['_id']}).update({'$set': {'createdDate': new_date}}) count = count + 1 if count == 125: bulkOp.execute() bulkOp = collection.initialize_ordered_bulk_op() if count % 125 != 0: bulkOp.execute() |