【发布时间】:2022-01-19 17:15:05
【问题描述】:
我想根据客户的要求在firestore 中存储超过 500 个文档。在将文档写入(设置)到 firebase 时,我遇到了同样的问题。经过对网络的大量研究,我创建了自己的解决方案。
希望这对你有帮助..!
【问题讨论】:
标签: node.js google-cloud-firestore google-cloud-functions
我想根据客户的要求在firestore 中存储超过 500 个文档。在将文档写入(设置)到 firebase 时,我遇到了同样的问题。经过对网络的大量研究,我创建了自己的解决方案。
希望这对你有帮助..!
【问题讨论】:
标签: node.js google-cloud-firestore google-cloud-functions
此代码用于创建 5000 条虚拟记录。
const trailContacts = () => {
let names = [];
for (let index = 1; index < 5002; index++) {
names.push({
name: 'trail',
});
}
return names
};
这是将名字存储到firebase-firestore的代码。
let contacts = trailContacts();
let count = Math.ceil(contacts.length/500);
for (let index =0; index < count; index++) {
let splicedContacts = contacts.splice(0, 499);
let contactsBatch = db.batch();
splicedContacts.forEach((con) => {
let collection = {};
let name = con.name;
let collectionRef = db.collection('trail');
contactsBatch.set(collectionRef, collection);
});
await contactsBatch.commit();
}
【讨论】: