首页 > 解决方案 > 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")

标签: pythonmongodbpymongo

解决方案


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


推荐阅读