Google Cloud Functions Firestore 限制
Google Cloud Functions Firestore Limitations
我编写了一个函数,它在过去 24 小时内在 Firestore 中的所有更改文档中获取查询快照。我循环遍历此 Querysnapshot 以获取相关信息。我想将此文档中的信息保存到对每个用户来说都是唯一的地图中。每个用户平均每天生成 10 个文档。所以每张地图平均被写了 10 次。现在我想知道整个事情是否是可扩展的,或者是否会达到 Firebase 中规定的每笔交易 500 次写入限制,因为更多用户将使用该应用程序。
我所说的限制已记录在案 in Google documentation。
此外,我很确定我的代码真的很慢。所以我感谢每一次优化。
exports.setAnalyseData = functions.pubsub
.schedule('every 24 hours')
.onRun(async (context) => {
const date = new Date().toISOString();
const convertedDate = date.split('T');
//Get documents (that could be way more than 500)
const querySnapshot = await admin.firestore().collectionGroup('exercises').where('lastModified', '>=', `${convertedDate}`).get();
//iterate through documents
querySnapshot.forEach(async (doc) => {
//some calculations
//get document to store the calculated data
const oldRefPath = doc.ref.path.split('/trainings/');
const newRefPath = `${oldRefPath[0]}/exercises/`;
const document = await getDocumentSnapshotToSave(newRefPath, doc.data().exercise);
document.forEach(async (doc) => {
//check if value exists
const getDocument = await admin.firestore().doc(`${doc.ref.path}`).collection('AnalyseData').doc(`${year}`).get();
if (getDocument && getDocument.exists) {
await document.update({
//map filled with data which gets added to the exisiting map
})
} else {
await document.set({
//set document if it is not existing
}, {
merge: true
});
await document.update({
//update document after set
})
}
})
})
})
您问题中的代码不在 Firestore 上 use a transaction,因此不受限制 quote/link。
我仍然建议对您的查询设置一个限制,并以合理的批次(几百个是合理的)处理文档,这样您就不会给代码带来不可预测的内存负载。
我编写了一个函数,它在过去 24 小时内在 Firestore 中的所有更改文档中获取查询快照。我循环遍历此 Querysnapshot 以获取相关信息。我想将此文档中的信息保存到对每个用户来说都是唯一的地图中。每个用户平均每天生成 10 个文档。所以每张地图平均被写了 10 次。现在我想知道整个事情是否是可扩展的,或者是否会达到 Firebase 中规定的每笔交易 500 次写入限制,因为更多用户将使用该应用程序。
我所说的限制已记录在案 in Google documentation。
此外,我很确定我的代码真的很慢。所以我感谢每一次优化。
exports.setAnalyseData = functions.pubsub
.schedule('every 24 hours')
.onRun(async (context) => {
const date = new Date().toISOString();
const convertedDate = date.split('T');
//Get documents (that could be way more than 500)
const querySnapshot = await admin.firestore().collectionGroup('exercises').where('lastModified', '>=', `${convertedDate}`).get();
//iterate through documents
querySnapshot.forEach(async (doc) => {
//some calculations
//get document to store the calculated data
const oldRefPath = doc.ref.path.split('/trainings/');
const newRefPath = `${oldRefPath[0]}/exercises/`;
const document = await getDocumentSnapshotToSave(newRefPath, doc.data().exercise);
document.forEach(async (doc) => {
//check if value exists
const getDocument = await admin.firestore().doc(`${doc.ref.path}`).collection('AnalyseData').doc(`${year}`).get();
if (getDocument && getDocument.exists) {
await document.update({
//map filled with data which gets added to the exisiting map
})
} else {
await document.set({
//set document if it is not existing
}, {
merge: true
});
await document.update({
//update document after set
})
}
})
})
})
您问题中的代码不在 Firestore 上 use a transaction,因此不受限制 quote/link。
我仍然建议对您的查询设置一个限制,并以合理的批次(几百个是合理的)处理文档,这样您就不会给代码带来不可预测的内存负载。