Python,将mongodump的bson输出转换为json对象数组(字典) [英] Python, Convert bson output of mongodump to array of json objects (dictionaries)

查看:659
本文介绍了Python,将mongodump的bson输出转换为json对象数组(字典)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用mongodump命令转储了一个mongodb集合.输出是包含以下文件的转储目录:

I have dumped a mongodb collection using the mongodump command. The output is a dump directory which has these files:

dump/
    |___coll.bson
    |___coll.metadata.json

如何将导出的文件打开到可在python中工作的字典数组? 我尝试了以下方法,但都无济于事:

How can I open the exported files to a array of dictionaries that work in python? I tried the following and none worked:

with open('dump/coll.bson', 'rb') as f:
    coll_raw = f.read()
import json
coll = json.loads(coll_raw)

# Using pymongo
from bson.json_util import loads
coll = loads(coll_raw)

ValueError: No JSON object could be decoded

推荐答案

您应该尝试:

from bson import BSON
with open('dump/coll.bson', 'rb') as f:
    coll_raw = f.read()

coll = bson.decode_all(coll_raw) 

这篇关于Python,将mongodump的bson输出转换为json对象数组(字典)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆