MongoDB - 将查询结果保存到excel文件中

import pymongo
import re

client = pymongo.MongoClient('127.0.0.1', 27017)
db_name = 'Trade'

db = client[db_name]


filter1 = {'$or':[{"phone": {'$ne':""}}, {"cell_phone": {'$ne':""}}]}
filter2 = {'address':1, 'contact':1, 'phone':1, 'cell_phone':1, '_id':0}

results = db.business.find(filter1, filter2)

from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

ws.append(('地址', '联系人', '电话', '手机'))
# for _ in a:
#     print(a.next())

for data in results:
    address = data['address']
    contact = data['contact']
    phone = data['phone']
    cell_phone = data['cell_phone']
    result = (address, contact, phone, cell_phone)
    ws.append(result)

wb.save("sample.xlsx")

转载于:https://www.cnblogs.com/allen2333/p/9580310.html

你可能感兴趣的:(MongoDB - 将查询结果保存到excel文件中)