我目前正在将移动用户的电话联系人上传到 Firestore 数据库。
平均一个用户有大约 500 个电话号码,而我将有大约 1000 个用户。
我的结构如下:
Firestore-root
|
--- users_contacts (collection)
|
--- uid (document)
|
--- contacts (subcollection)
|
--- phoneNumberOfContact (document)
|
--- phoneNumberOfContact: true
我还有另一个集合,其中存储了我想要与特定用户的联系人进行比较的一般电话号码。
我那里有大约 50,000 个电话号码,每个电话号码都是一个文档。以后这个数字会大大增加,可能会达到100万。
Firestore-root
|
--- db_contacts (collection)
|
--- phoneNumber (document)
|
--- phoneNumber: true
我正在尝试检查特定已知 uid 和 db_contacts 集合的常用号码。 db_contacts 集合中存在多少个已知 uid。
我的云功能如下:
- Fetch all the Phone Numbers of a Contact
- 首先,我只想获取用户文档的 id,因为 id 是电话号码,希望它能让过程更快。但在 Javascript 中似乎不可能,因为 snapshotChanges() 不存在。
- 循环遍历获取的联系人并检查 db_contacts 中是否存在该联系人。由于我已经知道检查它是否存在的参考路径,所以这应该会很快
- 返回所有常用联系人
如果 JavaScript 中有 snapshotChanges() 的替代方案,我的脚本将会运行得更快。我的思维方式正确吗?
到目前为止我做了什么:
exports.findCommonNumbers = functions.https.onCall((data, context) => {
return new Promise((resolve, reject) => {
fetchCommonNumbers().then((commonNumbers) => {
console.log(commonNumbers);
resolve("Done");
}).catch((err) => {
console.log(err);
reject("Error Occured");
});
});
});
async function fetchCommonNumbers() {
var commonNumbers = [];
let contactsReference = admin.firestore().collection("user_contacts").doc("iNaYVsDCg3PWsDu67h75xZ9v2vh1").collection("contacts");
const dbContactReference = admin.firestore().collection('db_contacts');
userContacts = await contactsReference.get();
userContacts = userContacts.docs;
for(var i in userContacts){
var userContact = userContacts[i];
const DocumentID = userContact.ref.id;
//Check if Document exist
dbContact = await dbContactReference.doc(DocumentID).get();
if (dbContact.exists) {
commonNumbers.push(DocumentID);
}
}
return Promise.resolve(commonNumbers);
}
函数 findCommonNumbers 的执行时间为 60 秒。它必须要快得多。我怎样才能让它更快?