I have a MongoDB database where I overwrote the _id
field manually using some generated tags (duplicates are not an issue).
Trouble is, I am trying to find_one
searching by _id
but it continuously returns None
and I cannot figure it out for the life of me. Any help would be appreciated.
Here is the code I have so far:
async def update(self):
#self.input is part of my class variables and comes in as a string
client = AsyncIOMotorClient(sensitive_data.mongoDB_server_address) #sensitive data is another python file with the server address/usernae/password
db = client.Database
accounts = db.accounts
print(type(self.input)) #this prints string
account = await accounts.find_one({'_id':self.input})
print(account) #this prints none
return account
Edit:
I found some information on pyMongo documentation about ObjectID
. I tried implementing it with this code:
from bson.objectid import ObjectId
async def update(self):
#self.input is part of my class variables and comes in as a string
client = AsyncIOMotorClient(sensitive_data.mongoDB_server_address) #sensitive data is another python file with the server address/usernae/password
db = client.Database
accounts = db.accounts
print(type(self.input)) #this prints string
account = await accounts.find_one({'_id':ObjectID(self.input)})
print(account) #this prints none
return account
But get this traceback:
bson.errors.InvalidId: 'mystring' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string