使用flask和python从mongo中提取多个记录

2024-10-06 10:01:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我还在这里学习,目前正在尝试使用FLASK来创建Restful接口

我要做的是从mongo数据库中提取一组记录。 我发现_one()工作得很好,现在正在研究如何在光标上书写

当我知道至少有5条记录时,这个代码只显示一条记录

@app.route('/oer/api/v1.0/getType/', methods = ['GET'])
def getType():

    # Prepare a dictionary object to hold the result of any processing
    result = {}

    # Get hold of DB connection
    db_connection = getDbConnection()
    db = db_connection['OER']
    # Extract all records passed for the paramater that matches "Type": ie MOOC
    oerType = request.args.get("oerType");
    # test to see that there is something in the string if so try to get a record
    # Not doing this test yet lets get it working with a known record first 

    for d in db.oer_records.find({"Type":oerType}): 
        result.update(make_public_page(d))

    return jsonify(result) 

所以它可以工作但只返回一个json文档而不是一个集合?我以为结果更新每次都会附加一个新记录 FYI make_public_page()删除BISON ID以允许jsonify工作。在

这就是它的回报

^{pr2}$

感谢任何帮助。在

谢谢


Tags: ofthetofordbgetthat记录
1条回答
网友
1楼 · 发布于 2024-10-06 10:01:26

当使用dict.update(dict2)时,您正在将dictionary dict2的键值对添加到dict中,这将导致一个大dict。您可能需要做的是创建dict列表。在

mylist = []
mylist.append(dict)

翻译成您的代码:

^{pr2}$

相关问题 更多 >