Firebase 使用散列(sha1)作为firestore文档的id是一个好主意吗?

Firebase 使用散列(sha1)作为firestore文档的id是一个好主意吗?,firebase,hash,google-cloud-firestore,sha1,bing-api,Firebase,Hash,Google Cloud Firestore,Sha1,Bing Api,我的设想如下: 我正在使用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,82e493e511734febfc

我的设想如下:

我正在使用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"
}
请注意,此对象中没有id字段,因此我临时创建了一个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 })
const CryptoJS = require("crypto-js");
const id = `${CryptoJS.SHA1(news.url)}`

await database.collection(`news`).doc(`${id}`).set(news, { merge: true })
当BING api返回相同的消息并带有更新日期时,此解决方案会变得效率低下,这会导致对象在我的firestore数据库中重复

我计划使用的解决方案

使用sha1算法将新闻url转换为哈希,如下所示:

const time = new Date(news.datePublished).getTime()
const id = `${language}${time}`

await database.collection(`news`).doc(`${id}`).set(news, { merge: true })
const CryptoJS = require("crypto-js");
const id = `${CryptoJS.SHA1(news.url)}`

await database.collection(`news`).doc(`${id}`).set(news, { merge: true })
该指南为以这种格式使用ID留下了余地。但我主要关心的是大id d40e5b8df6462e138fe617a84ddabae7f78360a6的性能,因为我将有至少5种语言的数千条新闻

记住:我需要根据一些对象属性创建可追踪的ID,因为可以从BING新闻中检索到一些内容相同、发布日期不同的新闻,然后我需要更新它们


我想知道是否有任何计数器点让我选择其他解决方案?

您可以使用Firestore的默认ID生成器功能。我很确定一个大的ID不会引起明显的性能问题,因此Google为什么要使用这样一个函数在他们的数据库中生成唯一的ID

以下是我提取的功能,并在我的项目中使用了很长一段时间:

const generateId=函数{ //字母数字字符 常量字符= ‘ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789’; 设autoId=; 对于设i=0;i<20;i++{ autoId+=chars.charAtMath.floorMath.random*chars.length; } 返回autoId; };
使用此函数几乎不可能在两个文档中遇到相同的ID,但您可以继续操作,并在结果中添加时间戳,这只是为了省心。

谢谢Ogulcan,但是我需要基于一些对象属性创建可跟踪的ID,因为可以从BING新闻中检索到一些内容相同、发布日期不同的新闻