简体   繁体   English

firebase 的谷歌云函数中的“获取”函数是否每次都读取每个文档?

[英]Are "get" functions in Google cloud functions for firebase reading each document everytime?

I observed a huge amount of read on my firebase console and I was wondering if this might come from my "referral function".我在 firebase 控制台上观察到大量读取,我想知道这是否来自我的“推荐功能”。

This function works perfectly fine but I was wondering whether or not this function could end up with a crazy load of read in case of app scaling.这个 function 工作得很好,但我想知道这个 function 是否会在应用扩展的情况下以疯狂的读取负载结束。

My question: does this function imply that every time a user comes in, it will account for a number of read equivalent to the number of users in my collection?我的问题:这个function是不是意味着每次有用户进来,都会占到我收藏的用户数等量的read?

Thus, as this function is an onUpdate, will it redo the job every time a document is updated?那么,由于这个 function 是一个 onUpdate,它会在每次更新文档时重做吗?

I would not mind some resources on the topic because I found it unclear on Firebase's website.我不介意关于该主题的一些资源,因为我发现它在 Firebase 的网站上不清楚。

I hope my questions are clear!我希望我的问题很清楚!

Thank you very much!非常感谢你!

export const onReferralInfoUpdate = functions.
firestore.document('users/{userUid}')
.onUpdate(async (change, context) => {
    const before = change.before.data();
    const after = change.after.data();
    const currentUserUid = after["uid"];

    if (before.godfather_code == after.godfather_code){
        console.log('Text did not change')
        return null
    }

    const godfatherUserSnapshot = await db.collection('users').where("referral_code", "==", after.godfather_code).get();
    const godfather = godfatherUserSnapshot.docs[0].data();
    const godfatherUid = godfather["uid"];
    
    const userRef = db.collection('users').doc(after.uid);

    const godfather_code = after.godfather_code
    
    await userRef.update({godfather_code})
    
    console.log(`the text before was >> ${before.godfather_code} << and after is ${after.godfather_code}` )

    let batch = db.batch();

    const updateGodfather = db.collection('users').doc(godfatherUid);

    batch.update(updateGodfather, {
        reward: admin.firestore.FieldValue.increment(100),
        godChildUid: admin.firestore.FieldValue.arrayUnion(currentUserUid),
    });

    return batch.commit();
    
});
    
    

Yes, the where("referral_code", "==", after.godfather_code).get() will fetch all the documents matching the query every time onUpdate() function triggers and you'll be charged N reads (N = number of matched documents).是的, where("referral_code", "==", after.godfather_code).get()将在每次onUpdate() function 触发时获取与查询匹配的所有文档,您将被收取 N 次读取费用(N = 数量匹配的文档)。 The Admin SDK doesn't have any caching like Client SDKs.管理员 SDK 没有像客户端 SDK 那样的任何缓存。

Does this function imply that every time a user comes in, it will account for a number of read equivalent to the number of users in my collection?这个function是不是意味着每进来一个用户,就会占到我收藏的用户数等量的read?

Not numbers of documents in the users collection, only the documents matching your query as mentioned.不是用户集合中的文档数量,只有与您的查询匹配的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM