python mongoenqine 查询文档
我正在从 mongodb 查询 2 个集合。我正在查询一个具有名为“quiz_id”字段的集合。我想搜索该字段的值(例如第一个 quiz_id 值是“M7001”)。在第二个集合中,我想从第一个查询中搜索“quiz_id”的值(它是从 0 到 10 的测验的分数),该查询是一个键并返回该值。这是我所拥有的:
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)”打印每个测验 ID 号。但我不知道如何搜索为键返回的 mongo 查询集。信息:
print (quiz_id.quiz_id)
<class 'flask_mongoengine.BaseQuerySet'>
任何帮助将不胜感激,谢谢
I am querying 2 collections from mongodb. I am querying one collection that has a field called 'quiz_id'. I want to search for the value of this field (e.g. the first quiz_id value is 'M7001'). In the second collection, I want to search for value of the 'quiz_id' (it's a score from a quiz taken from 0 to 10) from the first query which is a key and return the value. Here is what I have:
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")
So the first part 'print (quiz_id.quiz_id)' prints each of the quiz id numbers ok. but I can't figure out how to search the mongo queryset returned for the key. For information:
print (quiz_id.quiz_id)
<class 'flask_mongoengine.BaseQuerySet'>
Any help would be greatly appreciated, thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
quiz_chk
需要额外的迭代:[q [q ['quiz_id']对于quiz_chk中的q]
,它会使整个代码看起来像:
输出:输出:
quiz_chk
requires additional iteration:[q['quiz_id'] for q in quiz_chk]
which would make entire code look like that:
Output: