Firestore - batch.add 不是函数
Firestore - batch.add is not a function
Firestore 批量写入的 documentation 仅将 set()
、update()
和 delete()
列为允许的操作。
有没有办法在批处理中添加一个add()
操作?我需要使用自动生成的 ID 创建文档。
根据文档
Behind the scenes, .add(...) and .doc().set(...) are completely equivalent, so you can use whichever is more convenient.
也许这也适用于批次?
您可以分两步完成此操作:
// Create a ref with auto-generated ID
var newCityRef = db.collection('cities').doc();
// ...
// Add it in the batch
batch.set(newCityRef, { name: 'New York City' });
.doc()
方法不会向网络或磁盘写入任何内容,它只是使用自动生成的 ID 进行引用,您可以稍后使用。
创建对要添加批处理数据的集合的引用
我们使用 forEach 遍历 req.body 并使用 set 方法
设置要添加到集合中的每个数据
我们使用 commit 方法提交数据并将数据保存到集合中,并在成功时发送成功响应。
在我的例子中,使用 AngularFire2,我必须使用 batch.set() 方法,作为第一个参数传递带有先前创建的 ID 的文档引用,以及引用属性:
import { AngularFirestore } from '@angular/fire/firestore';
...
private afs: AngularFirestore
...
batch.set(
this.afs.collection('estados').doc(this.afs.createId()).ref,
er.getData()
);
对于PHP你可以试试:
$batch = $db->batch();
$newCityRef = $db->collection('cities')->newDocument();
$batch->set($newCityRef , [ 'name'=>'New York City' ]);
假设您有城市列表,并且您想批量写入它们。
final CityList = FirebaseFirestore.instance.collection('cities')
WriteBatch batch = FirebaseFirestore.instance.batch();
for(CityList city in cities) {
final newShoppingItem = ShoppingList.doc();
batch.set(newShoppingItem, {
'name': city.name,
'createdAt': DateTime
.now()
.millisecondsSinceEpoch
});
}
batch.commit();
Sam Stern 的回答是正确的方法,尽管如果您使用 AngularFire,.doc()
不能在没有参数的情况下使用来生成新的 docId(见 https://github.com/angular/angularfire/issues/1974).
AngularFire 这样做的方式是:
// Create a ref with auto-generated ID
const id = this.db.createId();
const newCityRef= this.db.collection("cities").doc(id);
// ...
// Add it in the batch
batch.set(newCityRef, { name: 'New York City' });
我将为 Firebase 9 提供一个答案,其中语法不同于 Firebase 8。
对于 Firebase 9,add()
相当于 addDoc()
,如 Firebase 9 上的 https://firebase.google.com/docs/firestore/manage-data/add-data#web-version-9_6 . It is for when you're not using batch nor transaction. As per the original problem posted, there is no equivalent of addDoc()
on batch nor transaction 所述。
我找到了一种方法,通过遵循答案 ,在 Firebase 9 上实现相当于 addDoc()
的批处理,如下所示:
const batch = writeBatch(db);
const docADocRef = doc(collection(db, "DocA"));
batch.set(docADocRef, {
fieldA: "This is field of an instance of DocA"
});
const docBDocRef = doc(collection(db, "DocB"));
batch.set(docBDocRef, {
docAID: docADocRef.id
});
batch.commit();
在此示例中,创建了 DocA 和 DocB 的实例,并且 DocB 接收指向 DocA 实例的指针。
这对我有用,PHP
的文档中提到了它
$batch = $db->batch();
# Set the data for NYC
$nycRef = $db->collection('samples/php/cities')->document('NYC');
$batch->set($nycRef, [
'name' => 'New York City'
]);
# Update the population for SF
$sfRef = $db->collection('samples/php/cities')->document('SF');
$batch->update($sfRef, [
['path' => 'population', 'value' => 1000000]
]);
# Delete LA
$laRef = $db->collection('samples/php/cities')->document('LA');
$batch->delete($laRef);
# Commit the batch
$batch->commit();
Firestore 批量写入的 documentation 仅将 set()
、update()
和 delete()
列为允许的操作。
有没有办法在批处理中添加一个add()
操作?我需要使用自动生成的 ID 创建文档。
根据文档
Behind the scenes, .add(...) and .doc().set(...) are completely equivalent, so you can use whichever is more convenient.
也许这也适用于批次?
您可以分两步完成此操作:
// Create a ref with auto-generated ID
var newCityRef = db.collection('cities').doc();
// ...
// Add it in the batch
batch.set(newCityRef, { name: 'New York City' });
.doc()
方法不会向网络或磁盘写入任何内容,它只是使用自动生成的 ID 进行引用,您可以稍后使用。
创建对要添加批处理数据的集合的引用 我们使用 forEach 遍历 req.body 并使用 set 方法
设置要添加到集合中的每个数据我们使用 commit 方法提交数据并将数据保存到集合中,并在成功时发送成功响应。
在我的例子中,使用 AngularFire2,我必须使用 batch.set() 方法,作为第一个参数传递带有先前创建的 ID 的文档引用,以及引用属性:
import { AngularFirestore } from '@angular/fire/firestore';
...
private afs: AngularFirestore
...
batch.set(
this.afs.collection('estados').doc(this.afs.createId()).ref,
er.getData()
);
对于PHP你可以试试:
$batch = $db->batch();
$newCityRef = $db->collection('cities')->newDocument();
$batch->set($newCityRef , [ 'name'=>'New York City' ]);
假设您有城市列表,并且您想批量写入它们。
final CityList = FirebaseFirestore.instance.collection('cities')
WriteBatch batch = FirebaseFirestore.instance.batch();
for(CityList city in cities) {
final newShoppingItem = ShoppingList.doc();
batch.set(newShoppingItem, {
'name': city.name,
'createdAt': DateTime
.now()
.millisecondsSinceEpoch
});
}
batch.commit();
Sam Stern 的回答是正确的方法,尽管如果您使用 AngularFire,.doc()
不能在没有参数的情况下使用来生成新的 docId(见 https://github.com/angular/angularfire/issues/1974).
AngularFire 这样做的方式是:
// Create a ref with auto-generated ID
const id = this.db.createId();
const newCityRef= this.db.collection("cities").doc(id);
// ...
// Add it in the batch
batch.set(newCityRef, { name: 'New York City' });
我将为 Firebase 9 提供一个答案,其中语法不同于 Firebase 8。
对于 Firebase 9,add()
相当于 addDoc()
,如 Firebase 9 上的 https://firebase.google.com/docs/firestore/manage-data/add-data#web-version-9_6 . It is for when you're not using batch nor transaction. As per the original problem posted, there is no equivalent of addDoc()
on batch nor transaction 所述。
我找到了一种方法,通过遵循答案 addDoc()
的批处理,如下所示:
const batch = writeBatch(db);
const docADocRef = doc(collection(db, "DocA"));
batch.set(docADocRef, {
fieldA: "This is field of an instance of DocA"
});
const docBDocRef = doc(collection(db, "DocB"));
batch.set(docBDocRef, {
docAID: docADocRef.id
});
batch.commit();
在此示例中,创建了 DocA 和 DocB 的实例,并且 DocB 接收指向 DocA 实例的指针。
这对我有用,PHP
的文档中提到了它 $batch = $db->batch();
# Set the data for NYC
$nycRef = $db->collection('samples/php/cities')->document('NYC');
$batch->set($nycRef, [
'name' => 'New York City'
]);
# Update the population for SF
$sfRef = $db->collection('samples/php/cities')->document('SF');
$batch->update($sfRef, [
['path' => 'population', 'value' => 1000000]
]);
# Delete LA
$laRef = $db->collection('samples/php/cities')->document('LA');
$batch->delete($laRef);
# Commit the batch
$batch->commit();