python将mongodb导出_python 导出mongoDB数据中的数据

这段Python代码展示了如何连接到MongoDB数据库,通过认证后,导出特定条件的数据到文本文件中。它使用了pymongo库,并根据时间范围筛选数据,最后将结果写入到一个以日期命名的TXT文件。
摘要由CSDN通过智能技术生成

import pymongo,urllib

import sys

import time

import datetime

reload(sys)

sys.setdefaultencoding('utf8')

from pymongo.connection import Connection

from datetime import timedelta

import os

connection=pymongo.Connection('127.0.0.1',27017)

db = connection.upload

db.authenticate("username","passowd")

format="%Y-%m-%d %H:%M:%S"

fileName="dstUrl"

datefmt="%m%d"

now = datetime.datetime.now().replace(day=1,hour=0,minute=0,second=0)

aDay = timedelta(days =-1)

endDate=now+aDay

startDate=endDate.replace(day=1)

endDateStr=endDate.strftime(format)

startDateStr=startDate.strftime(format)

startTime=int(time.mktime(time.strptime(startDateStr, '%Y-%m-%d %H:%M:%S'))*1000)

endTime=int(time.mktime(time.strptime(endDateStr, '%Y-%m-%d %H:%M:%S'))*1000)

#f=open(fileName, "w")

fileName=fileName+"("+startDate.strftime(datefmt)+"-"+endDate.strftime(datefmt)+").txt"

def findList(startTime,endTime):

print "startTime"+startDateStr+" endTime"+endDateStr

query={"completeTime":{"$gte":startTime,"$lt":endTime},"status":30,"videoCDNStatus":10,"checksumStatus":10,"mmsStatus":1}

encodeCursor=db.video_encode.find(query)

historyCursor=db.video_encode_history.find(query)

taskIterator(encodeCursor)

taskIterator(historyCursor)

def taskIterator(cusor):

for encode in cusor:

mid=encode["mid"]

encodeId=encode["encodeId"]

vtype=encode["vtype"]

dstUrl=encode["dstUrl"]

checksumPath=encode["checksumPtah"]

f.write(str(mid)+","+str(encodeId)+","+vtype+","+dstUrl+","+checksumPath+"\n")

print "start run to export dst url to file %s",fileName

if(os.path.isfile(fileName)):

print "file %s is exsis ,not create file",fileName

else:

f=open(fileName, "w")

findList(startTime,endTime)

f.close;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值