设置mongodb _id到firebase uid
我是新手,在堆栈溢出中问问题。
我正在实施一个应用程序,该应用程序正在使用Firebase Auth进行用户登录,注销和删除帐户,并使用Python(Pymongo)(Pymongo)来管理数据库。
当用户登录时,将在firebase auth中创建新用户,以便为新用户提供一个28个字符ID的新用户(它是字符串)。我的想法是将_id的值设置为从MongoDB集合到UID的值,但似乎UID比预期的要长: 在这里,您可以在Python控制台中看到输出(发生异常::'G2CGRPSTAOUWEFBPLZUJWME3OMR2'不是有效的objectid,它必须是12个字节输入或24个字符的hex字符串
)有解决这个问题的想法吗?我试过 “ _ ID”:创建文档时,ObjectId(str.Encode(id))
发生了一个例外:: ID必须是(字节,str,objectID)的实例,而不是< class'bytes'>
,而且我已经用完了想法。
希望您能帮助我,我很高兴听到您的问题。 提前致谢。
编辑
当我在前端使用Firebase Auth时 @dharmaraj建议我在客户端使用Firebase Auth Admin SDK,以便我可以根据MongoDB数据库中的用户_id分配某个UID。感谢您的建议!
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 saysAn 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.
EDIT
As I'm working with Firebase Auth in the frontend (react native + expo app)
@Dharmaraj suggested me to use Firebase Auth admin SDK in the client side so that I can assign a certain uid to the user based on the user _id in mongodb database. Thanks for your suggestion!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MongoDB对象ID是12个字节十六进制字符串,但Firebase Auth的UID不是。您可以将firebase UID作为字符串存储在文档中的其他字段中,或者因为您使用的是MongoDB,所以我认为您必须在服务器端运行此逻辑。在这种情况下,您可以首先在mongoDB中添加用户文档,并将该_id用作firebase uid。使用Firebase Admin SDK时,您可以指定此自定义UID
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
_id
字段是强制性的,在集合中唯一,并且不可变。但是,它确实不是必须是objectid
。通常是,因为MongoDB会自动创建字段,但要注意上面的警告,您可以将其设置为字符串或其他任何有效的BSON类型。
更多信息: https://wwww.mongodb.com/ DOC/MANUAL/CORE/DOMIST/#THE-_ID-FIELD
The
_id
field is mandatory, unique within a collection, and immutable. However it does not have to be anObjectID
.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