时间戳作为 firebase firestore 文档中的键

问题描述 投票:0回答:1

我想将数据保存在 Firebase Firestore 上,其中密钥为时间戳:

await FirebaseFirestore.instance
      .collection("users")
      .doc(uid)
      .collection('data')
      .doc(device.id)
      .update({
    DateTime.now().toString(): "${result['humidity']}_${result['tempInC']}"
  });
}

如图所示,这种嵌套层次结构的可能原因是什么:Screenshot,有什么解决方案吗?

firebase google-cloud-platform google-cloud-firestore
1个回答
0
投票

根据我在评论部分的第一个问题:

您需要将 Date 对象保存为 Firestore 文档中的键吗?

你的答案:

是的@AlexMamo

请注意,您无法将日期/时间戳对象保存为 Firestore 文档中的键。键是“always”字符串。您确实可以将 Date 对象转换为 String,但我认为这不是您正在寻找的。 我能想到的最佳选择是将时间戳保存为值而不是键。例如,您可以添加一个名为

addedAt

的字段,该字段应包含 Firestore 时间戳。通过这种方式,您将能够使用 Timestamp 对象查询集合。

    

© www.soinside.com 2019 - 2024. All rights reserved.