我目前正在从事一个项目,我已经在mongodb中创建了两个集合。 我希望能够从集合中获取特定领域 这是代码
app = mongo.db.apps
find_app = app.find({'email':session['email']})
x = []
for apps in find_app:
x.append(apps)
print(x)
这将打印[{'_id':ObjectId('5bb07458e58fb90fc8a60d49'),'email':'celviuks@ge.com','app_name':'apply','version':'1'}, {'_id':ObjectId('5bb0795ae58fb90c18bf7f5e'),'email':'celvius@egg.com','app_name':'egg','version':'1'}]
,我只需要app_name字段 请问我如何实现这一目标?
答案 0 :(得分:1)
我用更少的代码解决了
app = mongo.db.apps
find_app = app.find({'email':session['email']}, {app_name: 1, _id: 0})
done_apps = [doc['appname'] for doc in find_app]
return render_template('somepage.html',done_apps=done_apps)
答案 1 :(得分:0)
尝试一下:
app = mongo.db.apps
find_app = app.find({'email':session['email']},
{app_name: 1, _id: 0})
x = []
for apps in find_app:
x.append(apps)
print(x)