2015-12-16 88 views
3

我使用mongodump命令转储了一个mongodb集合。输出是具有这些文件的转储目录:Python,将mongodump的bson输出转换为json对象数组(字典)

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

如何我可以将导出的文件打开,在蟒蛇的工作字典的阵列? 我尝试以下,没有工作:

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 

回答

3

你应该尝试:

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

coll = bson.decode_all(coll_raw) 
+0

试过了,我得到了'InvalidBSON:坏eoo'错误 – CentAu

+0

这可能意味着你的BSON不正确,你能给我发送一个你想要解码的BSON样本对象吗? –

+0

bson文件是我用'mongodump'获得的转储文件。该文件是巨大的。让我看看是否可以通过小型数据库复制错误。 – CentAu

0

我知道这是回答了一个很久以前,但你可以尝试每个文件进行单独解码,然后你会知道哪个文档导致问题。

我使用这个库:https://github.com/bauman/python-bson-streaming

from bsonstream import KeyValueBSONInput 
f = open("restaurants.bson", 'rb') 
stream = KeyValueBSONInput(fh=f) 
for dict_data in stream: 
    print dict_data 
f.close() 

我看到25359个记录这一切似乎解码喜欢的东西:

{u'_id': ObjectId('5671bb2e111bb7b9a7ce4d9a'), 
u'address': {u'building': u'351', 
       u'coord': [-73.98513559999999, 40.7676919], 
       u'street': u'West 57 Street', 
       u'zipcode': u'10019'}, 
u'borough': u'Manhattan', 
u'cuisine': u'Irish', 
u'grades': [{u'date': datetime.datetime(2014, 9, 6, 0, 0), 
       u'grade': u'A', 
       u'score': 2}, 
      {u'date': datetime.datetime(2013, 7, 22, 0, 0), 
       u'grade': u'A', 
       u'score': 11}, 
      {u'date': datetime.datetime(2012, 7, 31, 0, 0), 
       u'grade': u'A', 
       u'score': 12}, 
      {u'date': datetime.datetime(2011, 12, 29, 0, 0), 
       u'grade': u'A', 
       u'score': 12}], 
u'name': u'Dj Reynolds Pub And Restaurant', 
u'restaurant_id': u'30191841'}