获取子集合中的Firestore Parent Doc ID
是否可以在子收集中检索父文档ID?
用户可以创建一个机器人,该机器人本身将包含链接到该机器人的历史记录。 要针对机器人ID列出此历史记录,我需要能够获得父级的ID(Bot的文档的ID)。
云功能中的最小逻辑:
try {
await db.collection("bots").add({
createdBy: uid,
createdAt: new Date(),
});
// create orders_history subcollection
await db.collection("bots").doc().collection("order_history").add({
createdBy: uid, // user uid
botId: context.ref.parent, // how i can get parent doc ID (7aIvUIjC...) ?
})
} catch (e) {
...
}
我的安全规则还应检查是否是否 BOTID
等于父文档的ID(7aivuijc ....)。
match /{path=**}/order_history/{id} {
allow read, write: if request.auth != null id == resource.data.botId;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看,您可以看到它记录了新文档的ID:
您可以使用相同的逻辑,使用呼叫中的返回值到
添加
,以获取子集合所需的ID:If you look at the second snippet in the documentation on adding a document, you can see that it logs the ID of the new document:
You can use the same logic, using the return value from the call to
add
, to get the ID that is needed for your subcollection: