基于时间戳选择并使用零更新时间戳

时间:2015-10-21 23:37:19

标签: python mongodb datetime mongodb-query pymongo

如何从Mongodb集合中的时间(HH:MM:SS.Milisecond)值大于零的日期字段中选择记录,并通过保留日期值将其更新为时间(HH:MM:SS)值为零与python脚本中的相同。

当前数据如下所示 -

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脚本中将时间戳更新为零 -

更新后的数据如下所示 -

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")

2 个答案:

答案 0 :(得分:3)

[![//Draw the Interior let center = CGPoint(x: bounds.width/2, y: bounds.height/2) //Calculate the radius based on the max dimension of the view. let radius = max(bounds.width, bounds.height) //Thickness of the Arc let arcWidth: CGFloat = 76 //Start and End of Circle angle let startAngle: CGFloat = 3 * π/4 let endAngle: CGFloat = π/4 let path = UIBezierPath(arcCenter: center, radius: radius/2 - arcWidth/2, startAngle: startAngle, endAngle: endAngle, clockwise: true) path.lineWidth = arcWidth counterColor.setStroke() path.stroke()] 由PyMongo表示为ISODate()个对象。 MongoDB假定日期和时间是UTC。对于给定的UTC时间datetime,有几种方法可以获得午夜(一天的开始):

d

答案 1 :(得分:1)

更新文档的最佳方式和00:00:00时间createdDate正在使用set模块,因为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() 在python中是datetime所以你可以使用日期时间实例属性datetime objectdayyear

{{1}}