每次将新元素添加到源数组时,如何自动将唯一字段附加到引用数组的元素?

发布于 2025-01-15 21:15:03 字数 821 浏览 2 评论 0原文

这是我所追求的一个简单示例:

/* source array in clues collection: */ 
clues = [
  { title: "Islero", answer: 5 },
  { title: "Miura", answer: 17 },
];

/* user collection: */
{
 name: "Bob",
 userClues: [
  {/ref to clues[0]/, solved: true},
  {/ref to clues[1]/, solved: false}
]}

{
 name: "Wallace",
 userClues: [
  {/ref to clues[0]/, solved: true},
  {/ref to clues[2]/, solved: true}
]}

主要“线索”数组将根据需要进行更新和修改,添加或删除线索并更改其值。每个用户文档中的“userClues”数组是通过引用精确镜像源线索数组,但我想向每个元素添加一个额外的“已解决”字段,该字段对于每个“userClues”数组中的每个元素都是唯一的。 “已解决”字段不链接或引用任何内容,每个字段都是独立的。在此示例中,将第三个元素添加到“clues”数组会将第三个元素添加到每个“userClues”数组以及每个用户自己的已解决字段。删除第三条线索也会从每个“userClues”数组中删除整个第三个元素。

我认为可以使用模式将“已解决”字段自动附加到每个“userClues”元素,但我不确定如何通过引用将主要“clues”数组传播到“userClues”数组中。我见过的每个引用示例都使用“_id”来链接元素,因此我不知道架构如何应用于指针。非常感谢任何针对此问题的建议或替代解决方案。

Here's a simple example of what I'm after:

/* source array in clues collection: */ 
clues = [
  { title: "Islero", answer: 5 },
  { title: "Miura", answer: 17 },
];

/* user collection: */
{
 name: "Bob",
 userClues: [
  {/ref to clues[0]/, solved: true},
  {/ref to clues[1]/, solved: false}
]}

{
 name: "Wallace",
 userClues: [
  {/ref to clues[0]/, solved: true},
  {/ref to clues[2]/, solved: true}
]}

The primary "clues" array is to be updated and modified as needed, adding or removing clues and changing their values. The "userClues" array in each user's document is to exactly mirror the source clues array by reference, but I'd like to add an additional "solved" field to each element that is unique to each element in each "userClues" array. The "solved" field isn't linked or referenced to anything, each one stands alone. In this example, adding a third element to the "clues" array would add a third element to each "userClues" array along with it's own solved field for each user. Removing this third clue would also remove the entire third element from each "userClues" array.

I'm thinking a schema could be used to append the "solved" field automatically to each "userClues" element, but I'm not sure how you could spread the primary "clues" array into the "userClues" array by way of reference. Every example of referencing I've seen uses the '_id' to link the elements, so I don't see how a schema could apply to a pointer. Any advice or alternative solutions to this problem are greatly appreciated.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文