I'm new to asking questions in Stack overflow.
I'm implementing an application that it's using Firebase Auth for users login, logout and delete account and mongodb with python (pymongo) to manage the database.
When a user signs in, a new user is created in Firebase Auth so the new user is given a new UID which is a 28 characters id (it is a string). My idea is to set the value of the _id from mongodb collection to the uid but it seems that the uid is longer than what it is expected:
Here you can see the output in the python console (An exception occurred :: 'g2CgrpStaOUwEFBpLzuJWme3OMR2' is not a valid ObjectId, it must be a 12-byte input or a 24-character hex string)
Do you have any idea to solve this problem? I tried
"_id": ObjectId(str.encode(id))
when creating the document but it says
An exception occurred :: id must be an instance of (bytes, str, ObjectId), not <class 'bytes'>
and I'm running out of ideas.
I hope you can help me, I'm glad to hear your questions. Thanks in advance.
CodePudding user response:
The MongoDB object ID is a 12 bytes hex string but Firebase auth's UID is not. You can either store the Firebase UID as string in a different field in document e.g. userId or since your are using MongoDB I assume you must be running this logic on server side. In that case you can first add the user document in MongoDB and use that _ID as Firebase UID. You can specify this custom UID when using Firebase admin SDK
CodePudding user response:
The _id
field is mandatory, unique within a collection, and immutable. However it does not have to be an ObjectID
.
It often is, as MongoDB will create the field automatically as one, but noting the caveats above, you can set it to a string or indeed any other valid BSON type.
More info: https://www.mongodb.com/docs/manual/core/document/#the-_id-field