获取子集合中的Firestore Parent Doc ID

发布于 2025-02-13 23:07:06 字数 858 浏览 3 评论 0 原文

是否可以在子收集中检索父文档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;
}

Is it possible to retrieve the parent document ID within a sub-collection?

The user can create a bot, which will itself contain a history linked to this bot.
To list this history against the bot ID., i need to be able to get the ID of the parent (that of the bot's document).

enter image description here

The minimal logic in cloud functions:

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) {
  ...
}

My security rule should also check if botId is equal to the id of the parent document (7aIvUIjC....).

match /{path=**}/order_history/{id} {
  allow read, write: if request.auth != null id == resource.data.botId;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-02-20 23:07:07

如果您查看,您可以看到它记录了新文档的ID:

// Add a new document with a generated id.
const res = await db.collection('cities').add({
  name: 'Tokyo',
  country: 'Japan'
});

console.log('Added document with ID: ', res.id);

您可以使用相同的逻辑,使用呼叫中的返回值到添加,以获取子集合所需的ID:

const res = await db.collection("bots").add({
  createdBy: uid,
  createdAt: new Date(),
});

// create orders_history subcollection 
await db.collection("bots").doc(res.id).collection("order_history").add({
  createdBy: uid, // user uid
  botId: context.ref.parent, // how i can get parent doc ID (7aIvUIjC...) ?
})

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:

// Add a new document with a generated id.
const res = await db.collection('cities').add({
  name: 'Tokyo',
  country: 'Japan'
});

console.log('Added document with ID: ', res.id);

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:

const res = await db.collection("bots").add({
  createdBy: uid,
  createdAt: new Date(),
});

// create orders_history subcollection 
await db.collection("bots").doc(res.id).collection("order_history").add({
  createdBy: uid, // user uid
  botId: context.ref.parent, // how i can get parent doc ID (7aIvUIjC...) ?
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文