tamaramaria
tamaramaria

Reputation: 193

bson.errors.InvalidId pymongo

I'm working with mongodb and PyMongo and I want to delete a row by id:

_id = "c8c9447c42c82044be595b" #row id in database
result = database.collection.delete_one({"_id": ObjectId(_id)}) #delete one row with the given id
print(result.deleted_count) #1 true, 0 false

When I run my code I get the following error message:

bson.errors.InvalidId: 'c8c9447c42c82044be595b' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string

I already tried to convert my id to hex but it didn't work, still the same error

hexlify = codecs.getencoder('hex')
print(':'.join(hex(ord(x))[2:] for x in 'c8c9447c42c82044be595b'))

Any suggestions?

Upvotes: 0

Views: 3256

Answers (1)

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24009

As the message says, 'c8c9447c42c82044be595b' is not a valid ObjectId: it must be a 12-byte input or a 24-character hex string. You have provided only 22 characters. =)

Upvotes: 1

Related Questions