选择基于时间戳和更新时间戳为零 [英] select based on timestamp and update timestamp with zero
问题描述
如何从Mongodb集合的时间(HH:MM:SS.Milisecond)值大于零的日期字段中选择记录,并通过保留日期值将时间(HH:MM:SS)值更新为零与python脚本中存在的一样。
How do I select records from a date field which has time (HH:MM:SS.Milisecond) value greater than zero from Mongodb collection and update it with time (HH:MM:SS) value as zero by keeping date value as same as existing in python script.
当前数据如下所示 -
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")
如何仅使用mongodbsql选择行4,5,6,7,并在python脚本中将时间戳更新为零 -
How to select only rows 4,5,6,7 using mongodbsql and update it with timestamp as zero in python script -
更新数据如下所示 -
After update 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")
推荐答案
更新文档和 设置
时间到 00:00:00
正在使用 datetime 模块,因为 createdDate
是一个 datetime对象在python中,因此您可以使用datetime实例属性 day
, year
和 month
。
The best way to update your documents and set
the time to 00:00:00
is using the datetime module because createdDate
is a datetime object in python so you can use datetime instance attribute day
, year
and month
.
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()
这篇关于选择基于时间戳和更新时间戳为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!