最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

这个用例最有效的火库模型是什么?

IT培训 admin 10浏览 0评论

这个用例最有效的火库模型是什么?

在我的应用程序中,我目前有三个集合:

db_contacts

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

db_contacts包含我在head之前添加的手机列表。该集合稍后将包含约100万个电话号码。目前它只拥有50,​​000个phoneNumber(50,000个文档)。

users_contacts

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

users_contacts保存我应用中每个用户的所有电话号码。该应用程序将被大约10,000名用户使用,每个用户拥有约500个联系人。所以我将有10,000个uid文件,每个联系人子集合中都有~500个phoneNumberOfContact文件。

users_common_contacts

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

users_common_contacts包含用户拥有的已在db_contacts中的联系人。我在users_contacts onWrite上设置的事件挂钩上填充此集合。

我的用例

我需要创建一个额外的集合,用于存储users_common_contacts内用户的唯一联系人。仅与此用户的联系人与db_contacts有共同之处,而其他用户没有。

到目前为止我做了什么,结果证明是一个错误如下:

users_unique_contacts

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

我的问题

我无法正确填充此表,因为无法找到单个用户的唯一联系人。我填写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);
                        });;
                    }
                });
            });

        });

代码不起作用,uniqueContactsRef.where('phone', '==', DocumentID).get()永远不会返回任何值。

如何建模我的users_unique_contacts?

Update:

进一步说明

我的意思是:“我需要创建一个额外的集合,它存储user_common_contacts内部用户的唯一联系人。只有这个用户与db_contacts有什么联系,而没有其他用户拥有。”

让我们说db_contacts有以下数字:

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

用户A与db_contacts共同使用以下数字:

  • 111111
  • 222222
  • 555555
  • 444444
  • 123456

用户B与db_contacts共同使用以下数字:

  • 111111
  • 222222
  • 333333
  • 555555

现在,用户A是唯一拥有以下数字的人:

  • 444444
  • 123456

现在,用户B是唯一拥有以下数字的人:

  • 333333

因此,用户A的唯一编号是:

  • 444444
  • 123456

因此,用户B的唯一编号是:

  • 333333
回答如下:

由于db_contacts集合已包含所有用户的所有电话号码,因此您无需将用户电话号码的唯一性与db_contacts集合中存在的电话号码进行比较,您需要检查用户拥有的号码,是否存在于其他用户中联系人列表。换句话说,您需要检查一个数字与其他用户联系人相比是否唯一。因此,您需要获取用户拥有的每个号码,并检查其是否存在于其他用户联系人列表中。在这种情况下,您应该通过添加如下所示的新集合来稍微更改数据库模式:

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

请参阅,uid文档现在只包含一个映射,因此它可以放在1MiB中,这是文档的最大大小。这是您的查询应如下所示:

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

现在你只需要获取数据。如果snapshot存在,则意味着它不是唯一的,如果不存在则意味着它是唯一的。如果需要,您还可以将这些数字存储在数组中。因此,您可以自行决定哪种解决方案更适合您。

这个用例最有效的火库模型是什么?

在我的应用程序中,我目前有三个集合:

db_contacts

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

db_contacts包含我在head之前添加的手机列表。该集合稍后将包含约100万个电话号码。目前它只拥有50,​​000个phoneNumber(50,000个文档)。

users_contacts

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

users_contacts保存我应用中每个用户的所有电话号码。该应用程序将被大约10,000名用户使用,每个用户拥有约500个联系人。所以我将有10,000个uid文件,每个联系人子集合中都有~500个phoneNumberOfContact文件。

users_common_contacts

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

users_common_contacts包含用户拥有的已在db_contacts中的联系人。我在users_contacts onWrite上设置的事件挂钩上填充此集合。

我的用例

我需要创建一个额外的集合,用于存储users_common_contacts内用户的唯一联系人。仅与此用户的联系人与db_contacts有共同之处,而其他用户没有。

到目前为止我做了什么,结果证明是一个错误如下:

users_unique_contacts

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

我的问题

我无法正确填充此表,因为无法找到单个用户的唯一联系人。我填写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);
                        });;
                    }
                });
            });

        });

代码不起作用,uniqueContactsRef.where('phone', '==', DocumentID).get()永远不会返回任何值。

如何建模我的users_unique_contacts?

Update:

进一步说明

我的意思是:“我需要创建一个额外的集合,它存储user_common_contacts内部用户的唯一联系人。只有这个用户与db_contacts有什么联系,而没有其他用户拥有。”

让我们说db_contacts有以下数字:

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

用户A与db_contacts共同使用以下数字:

  • 111111
  • 222222
  • 555555
  • 444444
  • 123456

用户B与db_contacts共同使用以下数字:

  • 111111
  • 222222
  • 333333
  • 555555

现在,用户A是唯一拥有以下数字的人:

  • 444444
  • 123456

现在,用户B是唯一拥有以下数字的人:

  • 333333

因此,用户A的唯一编号是:

  • 444444
  • 123456

因此,用户B的唯一编号是:

  • 333333
回答如下:

由于db_contacts集合已包含所有用户的所有电话号码,因此您无需将用户电话号码的唯一性与db_contacts集合中存在的电话号码进行比较,您需要检查用户拥有的号码,是否存在于其他用户中联系人列表。换句话说,您需要检查一个数字与其他用户联系人相比是否唯一。因此,您需要获取用户拥有的每个号码,并检查其是否存在于其他用户联系人列表中。在这种情况下,您应该通过添加如下所示的新集合来稍微更改数据库模式:

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

请参阅,uid文档现在只包含一个映射,因此它可以放在1MiB中,这是文档的最大大小。这是您的查询应如下所示:

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

现在你只需要获取数据。如果snapshot存在,则意味着它不是唯一的,如果不存在则意味着它是唯一的。如果需要,您还可以将这些数字存储在数组中。因此,您可以自行决定哪种解决方案更适合您。

发布评论

评论列表 (0)

  1. 暂无评论