是否有替代方法对属于各种集合的多个文档执行一组写入?
有点像官方文档中对多个文档的批量写入。
Transactions and batched writes on FireStore Docs
举个例子;
WriteBatch batch = db.batch();
// Set the value of 'NYC' in 'cities' collection
DocumentReference nycRef = db.collection("cities").document("NYC");
batch.set(nycRef, map1);
// Set the value of 'ABC' in 'SomeOtherCollection' collection
DocumentReference otherRef = db.collection("SomeOtherCollection").document("ABC");
batch.set(otherRef,map2));
是否可以对不同的集合执行批量写入?
最佳答案
批处理操作可以跨集合进行。来自documentation on batched writes :
// Get a new write batch WriteBatch batch = db.batch(); // Set the value of 'NYC' DocumentReference nycRef = db.collection("cities").document("NYC"); batch.set(nycRef, new City()); // Update the population of 'SF' DocumentReference sfRef = db.collection("cities").document("SF"); batch.update(sfRef, "population", 1000000L); // Delete the city 'LA' DocumentReference laRef = db.collection("cities").document("LA"); batch.delete(laRef); // Commit the batch batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { // ... } });
由于您将要写入的文档传入 batch.set()
,因此您还可以将来自不同集合的文档传入每个调用。
关于android - FireStore 批量写入不同的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51126009/