首页 > 数据库 >python读取mongodb并写入文件

python读取mongodb并写入文件

时间:2023-03-15 09:33:05浏览次数:39  
标签:__ -% 读取 item python mongodb datetime ## time

 

#!/usr/bin/env python
#coding=utf-8

from pymongo import MongoClient
import time
from datetime import datetime



def chaxun_data():
    ##client = MongoClient("192.168.1.170:28001",maxPoolSize=None)
    url = "mongodb://%s:%s@%s" % ('ureadonly', 'ureadonly123', "192.168.1.170:28001/mgdb_message")
    conn = MongoClient(url)
    db = conn.mgdb_message
    collection = db["t_push_record"]
    ##x = collection.find_one()

    with open ('mytest.txt', 'wb') as f:
#接下来可实现提取想要的字段内的数据
        for item in collection.find({"update_time": { "$gte" : datetime(2023,3,14), "$lt" : datetime(2023,3,15) } }).limit(10):
            ##print(item)
            f.write(str.encode(str(item["_id"])))
            f.write(str.encode('\n'))
    f.close()


if __name__ == '__main__':
    print("开始时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    l_flag = chaxun_data()
    print("结束时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

 

标签:__,-%,读取,item,python,mongodb,datetime,##,time
From: https://www.cnblogs.com/hxlasky/p/17217318.html

相关文章