2017-11-18 121 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!祝你好運! –

相關問題