【发布时间】:2020-05-28 04:57:36
【问题描述】:
我的场景如下:
-
我正在使用 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字段,因此我通过将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 }) 当从 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