我想编写一个脚本来从我的 mongoDB 数据库生成一个 CSV 文件,我想知道最方便的版本!
首先让我从集合的结构开始。
MyDataBase -> setting
users
fruits
在设置中我有类似的东西
setting -> _id
data
_tenant
而我所追求的,是从数据 中的配置文件中制作一个 CSV 文件,这些配置文件具有一些字段/属性,例如“姓名”、“地址”、“邮政编码”、“电子邮件”、年龄等,而不是全部这些配置文件具有所有文件/属性,甚至其中一些看起来像我根本不感兴趣的集合(有子分支)!
所以,我的代码是 python 到目前为止看起来像这些
myquery = db.settings.find() # I am getting everything !
output = csv.writer(open('some.csv', 'wt')) # writng in this file
for items in myquery[0:10]: # first 11 entries
a = list(items['data']['Profile'].values()) # collections are importent as dictionary and I am making them as list
tt = list()
for chiz in a:
if chiz is not None:
tt.append(chiz.encode('ascii', 'ignore')) #encoding
else:
tt.append("none")
output.writerow(tt)
这些字段/属性没有必要的所有字段,甚至其中一些是集合(带有子分支)并将作为字典导入!所以,我必须将它们转换为列表和所有,在这样的过程中需要注意的事情很少,而且看起来并不那么简单!
我的问题可能听起来很笼统,但这是制作此类报告的典型方式吗?如果没有,你能说清楚吗?