2017-11-18 172 views
0

刚开始与公司的FireStore并使用SetOptions.merge()在云公司的FireStore交易这样没有什么特别的我猜:为什么使用SetOptions.merge()时,我的Cloud Firestore事务不会合并?

final Map<String, Object> visitorMap = new HashMap<>(); 
visitorMap.put(Visitor.NOTIFY_ON_CHAT_MESSAGE, true); 
visitorMap.put(Visitor.TIME, FieldValue.serverTimestamp()); 
final DocumentReference docRefVisitor = mFirestore 
     .collection(VISITORS) 
     .document(theId) 
     .collection(VISITORS_USER) 
     .document(getCurrentUser().getUserId()); 
mFirestore.runTransaction(new com.google.firebase.firestore.Transaction.Function<void>() { 
    @Nullable 
    @Override 
    public void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException { 
     transaction.set(docRefVisitor, visitorMap, SetOptions.merge()); 
    } 
}) 

文档说:

如果文件不存在,它将被创建。如果文档 确实存在,其内容将与新设 数据被覆盖,除非你指定的数据应该被合并到 现有文档

我遇到Visitor.NOTIFY_ON_CHAT_MESSAGEboolean被覆盖在云现有boolean Firestore数据库文档。我虽然SetOptions.merge()不会覆盖现有的值? 也许我错过了一些有关如何Transaction作品或者这是一个测试相关的事情,因为CF是测试版

回答

1

关于SetOptions merge()法作为official documentation说:

更改集的行为()调用只能更换在其数据参数中指定的值。从set()调用中省略的字段将保持不变。

所以SetOptions.merge()方法只会替换fieldPaths下的字段。任何未在fieldPaths中指定的字段都将被忽略并保持不变。

作为一个混淆,如果文档不存在,它将被创建。如果该文件确实存在,其内容will be overwritten与新提供的数据,除非你指定的数据应该被合并到现有的文件,内容如下:

// Update one field, creating the document if it does not already exist. 
Map<String, Object> data = new HashMap<>(); 
data.put("Visitor.NOTIFY_ON_CHAT_MESSAGE", true); 
docRefVisitor.set(data, SetOptions.merge()); 
+0

由于下一次,我会阅读文档更好 – Erik

+0

你”欢迎Erik!祝你好运! –

相关问题