【问题标题】:Firestore - Does the dot notation work with set()?Firestore - 点符号是否适用于 set()?
【发布时间】:2022-01-08 00:27:00
【问题描述】:

我正在尝试运行此代码:

function updateUserLimitations(userId, limitations, batchOrTransaction = undefined) {
  const userLimitationsRef = firestore
    .collection("users")
    .doc(userId)
    .collection("limitations")
    .doc("userLimitations");

  if (batchOrTransaction) {
    return batchOrTransaction.set(
      userLimitationsRef,
      limitations,
      { merge: true }
    );
  }

  return userLimitationsRef.set(limitations, { merge: true });
}

updateUserLimitations(userId, { "messages.totalMessages": admin.firestore.FieldValue.increment(1) });

但是……

而不是在我的数据库中获取此文档数据:

{ // Doc data
   messages: {
      initialDate: timestamp, // Date of the first message (I need to preserve it),
      totalMessages: 20,
   },
}

我得到:

{
   ...other doc data...,
   messages.totalMessages: 20,
}

我需要带有合并选项的集合,因为我正在更新并在文档不存在时创建...

有什么想法吗?我在这里做错了吗?

【问题讨论】:

  • 嗨。点表示法仅适用于地图。 “message.totalMesssages”不是地图。你应该这样做:{ message: {} } message['totalMessages'] = admin.firestore.FieldValue.increment(1)。另外,为什么在申请merge: true 时使用它?如果你使用合并,你可以直接写{ message: { totalMessages: 'yourValue' } }
  • @LucasDavidFerrero { merge: true } 是否也适用于更新对象的特定字段?我认为这只能通过点符号来实现。
  • limitations 包含什么?向我们展示价值。
  • @AlexMamo 我用值更新了问题
  • 我认为这可能对你有用@VictorMolina stackoverflow.com/questions/46597327/…

标签: javascript firebase google-cloud-firestore


【解决方案1】:

您可以使用对象来“设置”嵌套数据:

{
  messages: {
    totalMessages: 20,
  }
}

“点”表示法用于对嵌套数据应用更新。

【讨论】:

  • 对象本身有更多的数据。我正在使用点符号来更新特定字段。 BTW,我会试试,但我认为对象会重叠,而不是更新特定的字段。
  • 我的错,它也深度更新了对象字段。
【解决方案2】:

如果您只需要更新,请使用update() 函数而不是set()。所以请更改以下代码行:

return userLimitationsRef.set(limitations, { merge: true });

收件人:

return userLimitationsRef.update(limitations);

还有你的:

updateUserLimitations(userId, { "messages.totalMessages": admin.firestore.FieldValue.increment(1) });

它将按预期工作。如果你想在不存在的情况下添加,那么你确实应该使用set()函数。

【讨论】:

  • 主要问题是如果不存在文档,我需要创建文档,正如我在问题底部指定的那样。顺便说一句,没有点符号,似乎我也可以深入更新将 set() 与 { merge: true } 组合的对象!
  • @AlexMamo:update() 函数不采用 Alex 的写入选项,并且不能(最后我检查)不能用于创建文档。 firebase.google.com/docs/reference/js/v8/…
  • @FrankvanPuffelen 没有看到“如果文档不存在则创建”。当然,更新函数不包含第二个参数。复制粘贴时忘记删除。顾名思义,如果文档存在,它就会更新。不创建文档。
【解决方案3】:

我目前的实现

function updateUserLimitations(userId, limitations, batchOrTransaction = undefined) {
  const userLimitationsRef = firestore
    .collection("users")
    .doc(userId)
    .collection("limitations")
    .doc("userLimitations");

  if (batchOrTransaction) {
    return batchOrTransaction.set(
      userLimitationsRef,
      limitations,
      { merge: true }
    );
  }

  return userLimitationsRef.set(limitations, { merge: true });
}

我也可以深度更新文档消息字段,如下:

updateUserLimitations(
   userId,
   { 
      messages: {
        totalMessages: admin.firestore.FieldValue.increment(1),
      }
   }
);

它保留所有以前存在的对象字段并更新特定的。

{ // Doc data
  messages: {
    initialDate: timestamp, // PRESERVED!
    totalMessages: 21,
  },
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    相关资源
    最近更新 更多