根据官方文档文档,我使用 FieldValue 将文档字段设置为时间戳值,我的问题是:
如何向同一个文档添加多个时间戳值,比如我需要设置startDate和endDate以及docTimpStamp
我正在使用以下代码:
@ServerTimestamp Date time;
并添加文档:
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("startDate", FieldValue.serverTimestamp());
time.setTime (...); // here change the date to be the endDate
dataMap.put("endDate", FieldValue.serverTimestamp());
time.setTime (...); // here change the date to be the docTimeStamp
dataMap.put("docTimeStamp",FieldValue.serverTimestamp());
并且此解决方案不起作用,未找到具有相同值而非实时的数据。
我该如何实施这个过程?
我认为您将ServerTimestamp
注释的使用与静态serverTimestamp()
方法的使用混合在一起
该ServerTimestamp
注释“用于标记要填充服务器时间戳的时间戳字段。如果正在写入的 POJO 包含 @ServerTimestamp 注释字段的空值,它将被服务器生成的时间戳替换。”
在您的代码中,time
Date 对象没有包含 null 的 POJO 。
另一方面,当您dataMap.put("endDate", FieldValue.serverTimestamp());
告诉 Firestore 为endDate
.
因此,您发现三个值的时间相同是正常的,因为它们是同时写入数据库(准)的。
此外,请注意(例如)以下两行之间没有链接。
time.setTime (...); // here change the date to be the endDate
dataMap.put("endDate", FieldValue.serverTimestamp());
在第一行中,您设置了一个新值,time
但time
在第二行中没有使用。并且FieldValue.serverTimestamp()
不链接到time
对象,如上所述。
因此,总而言之,您可能必须完全控制要写入的值(与time.setTime(...);
您一样)并避免使用该serverTimestamp()
方法而使用时间对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句