Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用flask和python从mongo中提取多个记录_Python_Mongodb_Flask_Cursor - Fatal编程技术网

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

使用flask和python从mongo中提取多个记录,python,mongodb,flask,cursor,Python,Mongodb,Flask,Cursor,我还在这里学习,目前正在尝试使用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 processi

我还在这里学习,目前正在尝试使用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文档,而不是一个集合?我以为result.update每次通过时都会附加一条新记录 仅供参考,make_public_page()删除野牛ID以允许jsonify工作

这就是它的回报

{
    "Account No": 1,
    "Country/ continent": "Argentina",
    "Educational Level": "Schools",
    "Educational Level (ISCED)": "2|3",
    "End year": "",
    "Funders": "",
    "Geolocation": "",
    "Initiative HQ address": "",
    "Initiative HQ city": "",
    "Initiative URL": "http://www.gleducar.org.ar",
    "Type": "OER"
}
谢谢你的帮助


感谢当使用
dict.update(dict2)
时,您正在向dict添加dictionary dict2的键值对,从而生成一个大dict。您可能需要做的是创建一个dict列表

mylist = []
mylist.append(dict)
翻译成您的代码:

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

    # Prepare a dictionary object to hold the result of any processing
    result = []  # create a list instead of dict

    # 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.append(make_public_page(d))  # append to the list of dicts

    return jsonify(items=result) 

python和flask仍然非常新,您的意思是在result={}下添加mylist=[],然后添加mylist.append(make_public_page(d))?我得到了ValueError:dictionary update sequence元素#0的长度为36;2是必需的吗?谢谢你的意见@Richard将最后一行更改为
返回jsonify(items=result)
。我忘记了jsonify不能返回数组
make_public\u page(d)
返回一个dict,对吗?这就是诀窍,谢谢-现在需要整理空seraches的响应,以及如何将它们分为10个记录的批次,但作为一组打印会产生很大的不同!