【问题标题】:Can I avoid Firestore transaction when using firebase.firestore.FieldValue.serverTimestamp( ) with other FieldValue functions将 firebase.firestore.FieldValue.serverTimestamp( ) 与其他 FieldValue 函数一起使用时,我可以避免 Firestore 事务吗
【发布时间】:2020-07-08 17:32:35
【问题描述】:
所以我有两个问题。
- 如果我使用
firebase.firestore.FieldValue.serverTimestamp( ) 更新单个字段,是否需要事务或此函数将负责服务器上的原子更新?
- 如果我使用多个 FieldValue 函数一起更新单个对象,如下例所示,我可以在没有事务的情况下执行此操作,并且所有字段都会发生原子更新吗?
this.afs.doc('path').update({
date: firebase.firestore.FieldValue.serverTimestamp( ),
array: firebase.firestore.FieldValue.arrayUnion('value'),
count: firebase.firestore.FieldValue.increment(1)
});
【问题讨论】:
标签:
javascript
firebase
google-cloud-firestore
【解决方案1】:
如果我使用 firebase.firestore.FieldValue.serverTimestamp( ) 更新单个字段,是否需要事务或此函数将负责服务器上的原子更新?
就像任何其他文档写入一样,它是“原子的”,因为写入只发生一次,并且在接收到它时在服务器上计算时间戳。
如果我使用多个 FieldValue 函数一起更新单个对象,如下例所示,我可以在没有事务的情况下执行此操作,并且所有字段都会发生原子更新吗?
它仍然是“原子的”,因为只有一个文档写入了在服务器上计算的值。它们都是同时写的。如果客户端不参与的事务,FieldValue 令牌基本上都在内部起作用。