Let's say I have a MongoDB database where I want to check whether a user is logged in or not by using a cookie with the ID of an entry in the users
database. For simplicity, let's have it as a simple variable foo = bar
.
How can I do a find_one()
query to the database using the _id
as the query item?
Here's an example of what I have in mind, in code:
foo = bar
userLoggedIn = users.find_one({'_id':ObjectId(foo)})
print(userLoggedIn)
Make sure you're importing ObjectId
by including the following in your file: from bson.objectid import ObjectId
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.