简体   繁体   English

对于此用例,最有效的 Firestore 模型是什么?

[英]What is the most efficient firestore model for this use case?

In my app I currently have three collections:在我的应用程序中,我目前有三个集合:

db_contacts db_contacts

Firestore-root
    |
    --- db_contacts (collection)
         |
         --- phoneNumber (document)
              |
              --- 'phone': phoneNumber

db_contacts holds a list of mobile phone that I add before head. db_contacts包含我在 head 之前添加的手机列表。 The collection will contain about 1 million phone number later on.该集合稍后将包含大约 100 万个电话号码。 Currently it only holds 50,000 phoneNumber (50,000 documents).目前它只有 50,000 个电话号码(50,000 个文件)。

users_contacts users_contacts

Firestore-root
    |
    --- users_contacts (collection)
         |
         --- uid (document)
              |
              --- contacts (subcollection)
                   |
                   --- phoneNumberOfContact (document)
                          |
                          --- 'phone': phoneNumberOfContact

users_contacts holds all of the phone numbers of every user on my app. users_contacts保存了我的应用程序上每个用户的所有电话号码。 The app will be used by about 10,000 users each having ~500 contacts.该应用程序将被大约 10,000 名用户使用,每个用户拥有约 500 个联系人。 So I will have 10,000 uid document and each have ~500 phoneNumberOfContact document inside the contacts subcollection.所以我将有 10,000 个 uid 文档,每个都有 ~500 个 phoneNumberOfContact 文档在联系人子集合中。

users_common_contacts users_common_contacts

Firestore-root
    |
    --- users_common_contacts (collection)
         |
         --- uid (document)
              |
              --- contacts (subcollection)
                   |
                   --- phoneNumberOfContact (document)
                          |
                          --- 'phone': phoneNumberOfContact

users_common_contacts contains the contacts a user have that are already in the db_contacts . users_common_contacts包含用户已经在db_contacts中的联系人 I'm populating this collection on an event hook set on the users_contacts onWrite.我在 users_contacts onWrite 上设置的事件挂钩上填充此集合。

My Use Case我的用例

I need to create an extra collection, that stores the unique contacts of a user that are inside the users_common_contacts.我需要创建一个额外的集合,用于存储 users_common_contacts 中用户的唯一联系人。 What contacts only this user have in common with the db_contacts and that no other user have.只有该用户的联系人与 db_contacts 有哪些共同点,而其他用户没有。

What I did so far, and turned out to be a mistake is the following:到目前为止我所做的,结果证明是一个错误如下:

users_unique_contacts users_unique_contacts

Firestore-root
    |
    --- users_unique_contacts (collection)
         |
         --- uid (document)
              |
              --- contacts (subcollection)
                   |
                   --- phoneNumberOfContact (document)
                          |
                          --- 'phone': phoneNumberOfContact

My Problem我的问题

I couldn't correctly populate this table, as there's no way to find the unique contacts of a single user.我无法正确填充此表,因为无法找到单个用户的唯一联系人。 My code for populating users_unique_contacts is as follow:我用于填充users_unique_contacts 的代码如下:

exports.userCommonContactsListener =
    functions.firestore.document('users_common_contacts/{userID}/contacts/{documentID}')
        .onCreate((snap, context) => {
            const DocumentID = context.params.documentID;
            const UserID = context.params.userID;

            const uniqueContactsRef = admin.firestore().collection('users_unique_contacts').doc(UserID).collection("contacts");

            return new Promise((resolve, reject) => {

                uniqueContactsRef.where('phone', '==', DocumentID).get().then(contactSnapshot => {
                    if (contactSnapshot.size > 0) {
                        console.log(`Found Common Number in Unique ${contactSnapshot}`);
                        contactSnapshot.forEach(documentSnapshot => {
                            documentSnapshot.ref.delete();
                            console.log(`Deleted ${documentSnapshot.ref.id} as unique contact`);
                            resolve(`Deleted ${documentSnapshot.ref.id} as unique contact`);
                        });
                    } else {
                        var db_contacts = {}
                        db_contacts['phone'] = DocumentID;
                        db_contacts['timestamp'] = new Date();
                        uniqueContactsRef.doc(DocumentID).set(db_contacts, { merge: true }).then((res) => {
                            console.log(`Added ${DocumentID} as unique contact`);
                            resolve(`Added ${DocumentID} as unique contact`);
                        }).catch((err) => {
                            console.log(err);
                            reject("Error Removing Unique Contact", err);
                        });;
                    }
                });
            });

        });

The code is not working, the uniqueContactsRef.where('phone', '==', DocumentID).get() is never returning any values.代码不起作用, uniqueContactsRef.where('phone', '==', DocumentID).get()永远不会返回任何值。

How can I go in modeling my users_unique_contacts ?我怎样才能对我的users_unique_contacts进行建模?

Update:更新:

Further Explanation进一步说明

What do I mean by: "I need to create an extra collection, that stores the unique contacts of a user that are inside the users_common_contacts. What contacts only this user have in common with the db_contacts and that no other user have."我的意思是: “我需要创建一个额外的集合,用于存储 users_common_contacts 内用户的唯一联系人。只有该用户与 db_contacts 有哪些共同点,而其他用户没有。”

Lets say db_contacts have the following numbers:假设 db_contacts 有以下数字:

  • 111111 111111
  • 222222 222222
  • 333333 333333
  • 444444 444444
  • 555555 555555
  • 123456 123456

User A have in Common with db_contacts the following numbers:用户 A与 db_contacts 有以下共同点:

  • 111111 111111
  • 222222 222222
  • 555555 555555
  • 444444 444444
  • 123456 123456

User B have in Common with db_contacts the following numbers:用户 B与 db_contacts 有以下共同点:

  • 111111 111111
  • 222222 222222
  • 333333 333333
  • 555555 555555

Now User A is the only one to have in common the following numbers:现在,用户 A是唯一拥有以下数字的用户:

  • 444444 444444
  • 123456 123456

Now User B is the only one to have in common the following numbers:现在,用户 B是唯一拥有以下数字的用户:

  • 333333 333333

So User A 's unique numbers are:所以用户 A的唯一编号是:

  • 444444 444444
  • 123456 123456

So User B 's unique numbers are:所以用户 B的唯一编号是:

  • 333333 333333

Since the db_contacts collection already contains all phone numbers of all users, you don't need to compare the uniqueness of user phone numbers with what exist in db_contacts collection, you need to check if the numbers a user has, exist or not in other user contact list.由于db_contacts集合已经包含了所有用户的所有电话号码,因此您不需要将用户电话号码的唯一性与db_contacts集合中存在的号码进行比较,您需要检查用户的号码是否存在于其他用户中联系人列表。 With other words, you need to check if a number is unique compared with other user contacts.换句话说,您需要检查一个号码与其他用户联系人相比是否唯一。 So you need to get each number a user has and check if it exists in other user contact list.因此,您需要获取用户拥有的每个号码并检查它是否存在于其他用户联系人列表中。 In this case, you should change your database schema a little bit by adding a new collection like this:在这种情况下,您应该通过添加如下新集合来稍微更改数据库架构:

Firestore-root
   |
   --- allUsersNumbers (collection)
         |
         --- uid (document)
             |
             --- phoneNumbers (map)
                   |
                   --- phoneNumberOne: true
                   |
                   --- phoneNumberTwo: true

See, uid document now holds only a map, so it can fit in the 1MiB, which is the max size of the document.看, uid文档现在只包含一个地图,因此它可以容纳 1MiB,这是文档的最大大小。 This is how your query should look like:您的查询应如下所示:

allUsersNumbersRef.where('phoneNumbers.searchedPhoneNumber', '==', true);

Now you only need to get the data.现在你只需要获取数据。 If the snapshot exists it means that is not unique, if doesn't exist it means that is unique.如果snapshot存在则表示它不是唯一的,如果不存在则表示它是唯一的。 You can also store those numbers in an array if you want.如果需要,您还可以将这些数字存储在数组中。 So it's up to you to decide which solution is better for you.因此,由您决定哪种解决方案更适合您。

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

相关问题 将 object 的所有键转换为小写的最佳方法(最有效)是什么? - What's the best way (most efficient) to turn all the keys of an object to lower case? NodeJS:使用“ require”的最有效方法 - NodeJS: Most efficient way to use “require” Js:将对象数组排序为部分的最有效方法是什么? - Js: What is the most efficient way to sort an array of objects into sections? 从服务器向客户端发送图像的最有效方法是什么? - What is the most efficient way to send images from server to client? 发出类型声明的最有效方法是什么? - What's the most efficient way to emit type declarations? 在 MongoDB 中存储树数据结构的最有效方法是什么? - What is the most efficient way to store a tree data structure in MongoDB? 在NodeJS服务器之间发送文件的最有效方法是什么? - What is the most efficient way of sending files between NodeJS servers? 在字符串中获取最后一个换行符的最有效方法是什么 - What is the most efficient way to get the last line break in a string 在javascript中将整数转换为数字的小数部分的最有效方法是什么? - What is the most efficient way to convert an integer to the fractional part of a number in javascript? 在CouchBase上获取用户未投票的帖子的最有效方法是什么? - What is the most efficient approach to get posts that were not voted by an user, on CouchBase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM