python - Pymongo .find() query
问题描述
I'm currently learning coding & am extremely new to it In my program i am running this command
What i am trying to do is use two attributes to find a record. If the two attributes are correct it will allow the user to continue using the program.
What is happening is that querying this it only cares about the first input (name) and whether or not the second input is correct will continue as it finds the correct name.
I want my program to query using mongodb which user has the name eg.. "Admin" and job title "Store manager" i have this setup in the db already. So for example if they were to type name as "Admin" and title as "till op" it would then trigger the "User not found". Any help is welcomed.
name = input("Please enter your name ")
job = input("Please enter your ID ")
myquery = name
myquery2 = job
myquery3 = (myquery, myquery2)
print(myquery3)
docu = staff_collection.collection.find_one({"Full Name": myquery}, {"Job_Role": myquery2})
if docu is not None:
print(docu)
else:
print("User not found")
解决方案
As far as I can see your .find query parameter is wrong. Instead of :
docu = staff_collection.collection.find_one({"Full Name": myquery}, {"Job_Role": myquery2})
It should be :
docu = staff_collection.collection.find_one({"Full Name": myquery, "Job_Role": myquery2})
I didn't fully understand your question. This approach is making sure that "Full Name" is the same as myquery and "Job Role" is the same as myquery2 in mongo and if so, returns a document.
If you are interested in using $or operator please refer to it here.
It's not the official docs, but I think this one is explained better
Cheers
推荐阅读
- java - 了解 SpringBatch /Spring boot 应用程序中的 TaskExecutor
- angular - Angular 11:大型应用程序的文件夹或目录结构
- google-cloud-platform - gzip/非 gzip 本地/云位置之间的 gsutil rsync
- c# - 如何获取数据库值(Lambda 表达式)
- awk - awk:对最后一个单词的文本块进行分组在两个文本文件中都很常见
- c++ - 使用 QNetworkAccessManager 解析 youtube
- html - 表单未使用 Laravel 集合提交
- r - 在R中切换矩阵的列顺序
- python - 如何遍历可见的表单字段但在 Django 中遇到某个字段时执行某些操作?
- java - Android studio自动horizontalscrollview无限滚动直到结束然后重复