我还在这里学习,目前正在尝试使用FLASK创建Restful接口
我想要做的是从mongo数据库中提取一组记录。 我有find_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每次都会附加一条新记录 FYI make_public_page()删除BISON 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"
}
任何帮助表示感谢。
由于
答案 0 :(得分:1)
使用dict.update(dict2)
时,您将字典dict2的键值对添加到dict中,这会产生一个大字典。您可能想要做的是创建一个dicts列表。
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)