【问题标题】:Is it a good idea to use a hash (sha1) as id for a firestore document?使用哈希 (sha1) 作为 Firestore 文档的 id 是个好主意吗?
【发布时间】:2020-05-28 04:57:36
【问题描述】:

我的场景如下:

  1. 我正在使用 BING 新闻 api,该 api 的返回是以下对象的列表:

    {
        "name": "Eterna Resenha contará com as participações de Neto e Vampeta",
        "url": "https://www.terra.com.br/esportes/lance/eterna-resenha-contara-com-as-participacoes-de-neto-e-vampeta,82e493e511734febfcdfda6fbd22c105xjafr9k2.html",
        "image": {
            "contentUrl": "http://p2.trrsf.com/image/fget/cf/800/450/middle/images.terra.com/2020/05/27/5ece8e302d1fb.jpeg",
            "thumbnail": {
                "contentUrl": "https://www.bing.com/th?id=ON.4E1CF6986982B70A3D6009F435822EF2&pid=News",
                "width": 700,
                "height": 393
            }
        },
        "description": "Durante a quarentena, as lives tomaram conta do país, tentando arrecadar doações para ajudar quem sofre com o coronavírus...",
        "provider": [
            {
                "_type": "Organization",
                "name": "Terra"
            }
        ],
        "datePublished": "2020-05-28T00:00:00.0000000Z",
        "category": "Entertainment"
    }
    
  2. 请注意,该对象中没有id 字段,因此我通过将datePublished 字段转换为Date 并使用getTime 方法返回一个long 然后与新闻语言连接如下:

    const time = new Date(news.datePublished).getTime()
    const id = `${language}${time}`
    
    await database.collection(`news`).doc(`${id}`).set(news, { merge: true })
    
  3. 当从 BING api 返回具有更新日期的相同新闻时,此解决方案变得低效,导致对象在我的 firestore 数据库中重复。

我打算使用的解决方案

使用sha1算法将新闻网址转换为hash,如下所示:

const CryptoJS = require("crypto-js");
const id = `${CryptoJS.SHA1(news.url)}`

await database.collection(`news`).doc(`${id}`).set(news, { merge: true })

firestore document creation best practices 指南保留了以这种格式使用 id 的范围。但我主要关心的是大 id (d40e5b8df6462e138fe617a84ddabae7f78360a6) 的性能,因为我将拥有至少 5 种语言的数千条新闻。

记住:我需要创建可追踪的 ID(基于某些对象属性),因为可以从 BING 新闻中检索到具有相同内容和不同 datePublished 的某些新闻,然后我需要更新他们。

我想知道是否有任何反点让我选择另一种解决方案?

【问题讨论】:

  • 我认为这样做并不违反 Firestore 模式。至少,我在他们的文档中没有看到任何警告开发人员的内容。 Firebase 允许开发人员在 Firestore 和 RTDB 中定义自己的文档 ID。所以,我相信使用 sha1 (或其他与哈希相关的方法)是安全的。他们对使用您自己的文档标识符方法的唯一说法是avoid "hotspot"。但是哈希相关的方法不会创建热点。

标签: firebase hash google-cloud-firestore sha1 bing-api


【解决方案1】:

您可以使用 Firestore 的默认 ID 生成器功能。我很确定“大 ID”不会导致明显的性能问题,因此 Google 使用这样的功能在其数据库中生成唯一 ID。

这是我为我的项目提取并使用了很长时间的函数:

        const generateId = function () {
        // Alphanumeric characters
        const chars =
            'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        let autoId = '';
        for (let i = 0; i < 20; i++) {
            autoId += chars.charAt(Math.floor(Math.random() * chars.length));
        }
        return autoId;
    };

使用此功能几乎不可能出现两个文档的相同 ID,但您可以继续并在结果中添加时间戳,只是为了让您放心。

【讨论】:

  • 感谢 Ogulcan,但我需要创建可追踪的 ID(基于某些对象属性),因为可以从 BING 新闻中检索到具有相同内容和不同 datePublished 的一些新闻
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2013-04-27
  • 2017-12-18
  • 1970-01-01
  • 2011-04-05
  • 2017-10-05
  • 2011-01-28
相关资源
最近更新 更多