背景
我们在公司使用mongo存储数据,但是mongo是由dba团队维护,为了数据安全mongoexport工具无法使用,无奈只能编码实现,为了不造轮子,github上搜索到如下代码(感谢大神分享),做微调就可以完成任务。特此记录。
mongo_to_csv.py
注: 如果mongo数据有嵌套关系,可以是使用聚合生成临时数据集合,如:
db.data.aggregate([
{$project: {"models":1, "_id": 0}},
{ $unwind : "$models" },
{$project: {"id": {$arrayElemAt: ["$all_model", 0]}, "name": {$arrayElemAt: ["$all_model", 2]} }},
{ $out : "data_tmp" }
])
# @Author: xiewenqian <int>
# @Date: 2016-11-28T20:35:09+08:00
# @Email: wixb50@gmail.com
# @Last modified by: int
# @Last modified time: 2016-12-01T19:32:48+08:00
import pandas as pd
from pymongo import MongoClient
def _connect_mongo(host, port, username, password, db):
""" A util for making a connection to mongo """
if username and password:
mongo_uri = 'mongodb://%s:%s@%s:%s/%s' % (username, password, host, port, db)
conn = MongoClient(mongo_uri)
else:
conn = MongoClient(host, port)
return conn[db]
def read_mongo(db, collection, query={}, host='localhost', port=27017, username=None, password=None, no_id=True):
""" Read from Mongo and Store into DataFrame """
# Connect to MongoDB
db = _connect_mongo(host=host, port=port, username=username, password=password, db=db)
# Make a query to the specific DB and Collection
cursor = db[collection].find(query)
# Expand the cursor and construct the DataFrame
df = pd.DataFrame(list(cursor))
# Delete the _id
if no_id and '_id' in df:
del df['_id']
return df
if __name__ == '__main__':
df = read_mongo('db_test', 'db_collection', {}, '172.168.203.174', 10800)
df.to_csv('1.csv', index=False)
网友评论