本文介绍了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对象数组(字典)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 08:30