我有一个 MongoDB 数据库,我在其中_id
使用一些生成的标签手动覆盖了该字段(重复不是问题)。
麻烦的是,我正在尝试find_one
搜索,_id
但它会不断返回None
,我无法终生弄清楚。任何帮助,将不胜感激。
这是我到目前为止的代码:
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
编辑:
我在 pyMongo 文档中找到了一些关于ObjectID
. 我尝试使用以下代码实现它:
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
但是得到这个回溯:
bson.errors.InvalidId: 'mystring' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string