python mongoenqine 查询文档
python mongoenqine querying documents
我正在从 mongodb 查询 2 个集合。我正在查询一个包含名为 'quiz_id' 的字段的集合。我想搜索此字段的值(例如第一个 quiz_id 值是 'M7001')。在第二个集合中,我想从第一个查询中搜索 'quiz_id' 的值(这是从 0 到 10 的测验的分数),这是一个键和 return 值。这是我拥有的:
quizzes = Topic_maths.objects.order_by('quiz_id')
quiz_chk = User.objects.filter(username=current_user)
for quiz_id in quizzes:
print (quiz_id.quiz_id)
key = quiz_id.quiz_id
if key in quiz_chk:
print ("score: ", key.quiz_id)
else:
print ("no score")
所以第一部分 'print (quiz_id.quiz_id)' 打印每个测验编号 ok。但我不知道如何在 mongo 查询集 returned 中搜索密钥。咨询:
print (quiz_id.quiz_id)
<class 'flask_mongoengine.BaseQuerySet'>
任何帮助将不胜感激,谢谢
quiz_chk
需要额外的迭代:
[q['quiz_id'] for q in quiz_chk]
这将使整个代码看起来像这样:
from mongoengine import *
connect('test')
class TopicMaths(Document):
quiz_id = StringField()
class User(Document):
username = StringField()
quiz_id = StringField()
current_user = 'Bob'
quizzes = TopicMaths.objects.order_by('quiz_id')
quiz_chk = User.objects.filter(username=current_user)
for quiz_id in quizzes:
print(quiz_id.quiz_id)
key = quiz_id.quiz_id
if key in [q['quiz_id'] for q in quiz_chk]:
print("score: ", key)
else:
print("no score")
输出:
M7001
no score
M7002
score: M7002
M7003
no score
我正在从 mongodb 查询 2 个集合。我正在查询一个包含名为 'quiz_id' 的字段的集合。我想搜索此字段的值(例如第一个 quiz_id 值是 'M7001')。在第二个集合中,我想从第一个查询中搜索 'quiz_id' 的值(这是从 0 到 10 的测验的分数),这是一个键和 return 值。这是我拥有的:
quizzes = Topic_maths.objects.order_by('quiz_id')
quiz_chk = User.objects.filter(username=current_user)
for quiz_id in quizzes:
print (quiz_id.quiz_id)
key = quiz_id.quiz_id
if key in quiz_chk:
print ("score: ", key.quiz_id)
else:
print ("no score")
所以第一部分 'print (quiz_id.quiz_id)' 打印每个测验编号 ok。但我不知道如何在 mongo 查询集 returned 中搜索密钥。咨询:
print (quiz_id.quiz_id)
<class 'flask_mongoengine.BaseQuerySet'>
任何帮助将不胜感激,谢谢
quiz_chk
需要额外的迭代:
[q['quiz_id'] for q in quiz_chk]
这将使整个代码看起来像这样:
from mongoengine import *
connect('test')
class TopicMaths(Document):
quiz_id = StringField()
class User(Document):
username = StringField()
quiz_id = StringField()
current_user = 'Bob'
quizzes = TopicMaths.objects.order_by('quiz_id')
quiz_chk = User.objects.filter(username=current_user)
for quiz_id in quizzes:
print(quiz_id.quiz_id)
key = quiz_id.quiz_id
if key in [q['quiz_id'] for q in quiz_chk]:
print("score: ", key)
else:
print("no score")
输出:
M7001
no score
M7002
score: M7002
M7003
no score