在 Firestore 中保存持续时间的推荐方法是什么?
What is the recommended way of saving durations in Firestore?
我正在尝试在 Firestore 中保存持续时间。例如,用户已经进行了 4 分 44 秒的练习。此信息的推荐数据类型是什么?
如果 Firestore 增量运算符能够处理所使用的数据类型,那就太好了。我考虑过使用 Timestamp
,但在 Firebase SDK 中,增量方法需要一个数字,而 Timestamp
不是数字。
What is the recommended way of saving durations in Firestore?
因为您在谈论持续时间,所以解决方案是将其存储为数字,特别是 Integer
,即 supported data-type。在您的示例中,4 分 44 秒将存储为:
284
这是因为 4*60 + 44 = 284.
如果想取回小时数和分钟数,只需将上面的算法逆向即可。
It would be nice if the Firestore increment operator would work on the used data type.
Firestore 已经提供了这样的选项。例如,如果您想在现有持续时间中添加一个小时,您可以使用:
FieldValue.increment(3600) // 60 * 60
如果要按小时递减,只需使用:
FieldValue.increment(-3600)
I thought about using a Timestamp
时间戳不是持续时间。 Firestore 时间戳包含小至纳秒的时间单位。因此,Timestamp 对象包含年、月、日、小时、分钟、秒、毫秒和纳秒。这不会以任何方式解决您的问题。
我正在尝试在 Firestore 中保存持续时间。例如,用户已经进行了 4 分 44 秒的练习。此信息的推荐数据类型是什么?
如果 Firestore 增量运算符能够处理所使用的数据类型,那就太好了。我考虑过使用 Timestamp
,但在 Firebase SDK 中,增量方法需要一个数字,而 Timestamp
不是数字。
What is the recommended way of saving durations in Firestore?
因为您在谈论持续时间,所以解决方案是将其存储为数字,特别是 Integer
,即 supported data-type。在您的示例中,4 分 44 秒将存储为:
284
这是因为 4*60 + 44 = 284.
如果想取回小时数和分钟数,只需将上面的算法逆向即可。
It would be nice if the Firestore increment operator would work on the used data type.
Firestore 已经提供了这样的选项。例如,如果您想在现有持续时间中添加一个小时,您可以使用:
FieldValue.increment(3600) // 60 * 60
如果要按小时递减,只需使用:
FieldValue.increment(-3600)
I thought about using a Timestamp
时间戳不是持续时间。 Firestore 时间戳包含小至纳秒的时间单位。因此,Timestamp 对象包含年、月、日、小时、分钟、秒、毫秒和纳秒。这不会以任何方式解决您的问题。