从.bson文件为python恢复数据库

时间:2018-10-21 16:21:27

标签: python database mongodb bson

我尝试使用以下命令读取bson文件:

import bson
input_dir='/path/to/file/dir/'
bson_stats =open(os.path.join(input_dir,'stats.bson'),'rb')
bson_stats = bson.loads(bson_stats.read())

当我尝试访问bson_stats时,我仅获得一行数据,但没有获得全部数据。

这是我第一次玩bson文件。我的问题是:

  • 如何访问bson文件中的整个数据?

链接到stats.bson文件:

https://drive.google.com/file/d/1N71hRafOm_6xWNsUfxcr5Sm0fQwydd1a/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

BSON (Binary JSON) Encoding and Decoding

>>> with open('stats.bson', 'rb') as f:
...     data = bson.decode_all(f.read())
...     print data
[{u'CodeGare': u'057', u'TrainEnRetard': 0, u'GareJour': u'2018-03-23_057', u'TotalTrains': 0, u'Date': u'2018-03-23', u'_id': ObjectId('5ab5a312d909c48f43e26cb5')}, {u'CodeGare': u'063', u'TrainEnRetard': 4, u'GareJour': u'2018-03-23_063', u'TotalTrains': 4, u'Date': u'2018-03-23', u'_id': ObjectId('5ab5a312d909c48f43e26cb8')}, {u'CodeGare': u'067', u'TrainEnRetard': 4, u'GareJour': u'2018-03-23_067', u'TotalTrains': 4, u'Date': u'2018-03-23', u'_id': ObjectId('5ab5a312d909c48f43e26cbb')}, {u'CodeGare': u'071', u'TrainEnRetard': 4, u'GareJour': u'2018-03-23_071', u'TotalTrains': 4, u'Date': u'2018-03-23', u'_id': ObjectId('5ab5a312d909c48f43e26cbe')}, {u'CodeGare': u'077', u'TrainEnRetard': 3, u'GareJour': u'2018-03-23_077', u'TotalTrains': 3, u'Date': u'2018-03-23', u'_id': ObjectId('5ab5a312d909c48f43e26cc1')}