我想将数据保存在 Firebase Firestore 上,其中密钥为时间戳:
await FirebaseFirestore.instance
.collection("users")
.doc(uid)
.collection('data')
.doc(device.id)
.update({
DateTime.now().toString(): "${result['humidity']}_${result['tempInC']}"
});
}
根据我在评论部分的第一个问题:
您需要将 Date 对象保存为 Firestore 文档中的键吗?
你的答案:
是的@AlexMamo
请注意,您无法将日期/时间戳对象保存为 Firestore 文档中的键。键是“always”字符串。您确实可以将 Date 对象转换为 String,但我认为这不是您正在寻找的。 我能想到的最佳选择是将时间戳保存为值而不是键。例如,您可以添加一个名为
addedAt
的字段,该字段应包含 Firestore 时间戳。通过这种方式,您将能够使用 Timestamp 对象查询集合。