【问题标题】:pymongo json returns bson stringpymongo json 返回 bson 字符串
【发布时间】:2021-08-23 00:43:00
【问题描述】:

Pymongo db 我只是不确定如何让它只返回值。除了解析它返回的字符串以仅获取值之外,我真的想不出。

for col_name in col_list:
    col = db[col_name]
    for x in col.find( {}, {'name.details.stuff': 1, '_id': 0} ):
        for key, value in x.items():
            story = "the cat went after those %s/"%value
            print(story )

输出:

the cat went after those {'name': {'details': 'things'}}.

我希望脚本只返回内容而不是 json/bson 内容。

【问题讨论】:

    标签: python-3.x mongodb pymongo


    【解决方案1】:

    find()返回一个dicts的可迭代游标,所以你可以使用普通的字典操作来获取单个值;例如

    for x in col.find( {}, {'name.details': 1, '_id': 0} ):
        story = f"the cat went after those {x.get('name').get('details')}/"
        print(story)
    

    for x in col.find( {}, {'name.details': 1, '_id': 0} ):
        story = f"the cat went after those {x['name']['details']}/"
        print(story)
    

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 2018-08-26
      • 2011-03-08
      • 2017-01-07
      • 2013-08-16
      • 1970-01-01
      • 2014-11-03
      • 2020-07-10
      • 2010-12-04
      相关资源
      最近更新 更多