在Firestore中管理createdAt时间戳 [英] Managing createdAt timestamp in Firestore

查看:74
本文介绍了在Firestore中管理createdAt时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每天,我都会从外部零售商将产品导入 Google Cloud Firestore 数据库。



在此过程中,产品可以是新产品(将新文件添加到数据库中)或现有产品(现有文件将在数据库中更新)。 / p>

应该注意,我每天要导入大约一千万种产品,所以我在数据库中查询每种产品是否已经存在。



我当前正在使用设置了合并,这正是我需要的,因为它不存在时会创建一个文档或更新现有文档的特定字段。



现在的问题是,鉴于提供的字段将被更新,因此我如何实现createdAt时间戳,因此原始的createdAt tim estamp更新时会丢失吗?如果该字段已经存在于文档中,有什么方法可以不更新该字段?

解决方案

我建议您使用创建Firestore文档时将创建新的专用字段的Cloud Function。更新文档时,传递给 set()方法的对象中不应包含此字段。



这里是对云功能代码的建议:

  const函数= require('firebase-functions'); 

const admin = require('firebase-admin');

admin.initializeApp();


exports.productsCreatedDate = functions.firestore
.document('products / {productId}')
.onCreate((snap,context)=> {

返回snap.ref.set(
{
CalculationCreatedAt:admin.firestore.FieldValue.serverTimestamp()
},
{merge:true}

.catch(错误=> {
console.log(错误);
返回false;
});
});






基于以上的Bob Snyder评论,请注意,也可以这样做:

  const docCreatedTimestamp = snap.createTime; 
return snap.ref.set(
{
CalculationCreatedAt:docCreatedTimestamp
},
{merge:true}

.catch( ...)


Every day I am importing products from external retailers into a Google Cloud Firestore database.

During that process, products can be either new (a new document will be added to the database) or existing (an existing document will be updated in the database).

Should be noted that I am importing about 10 million products each day so I am not querying the database for each product to check if it exists already.

I am currently using set with merge, which is exactly what I need as it creates a document if it doesn't exist or updates specific fields of an existing document.

Now the question is, how could I achieve a createdAt timestamp given that provided fields will be updated, therefore the original createdAt timestamp will be lost on update? Is there any way to not update a specific field if that field already exists in the document?

解决方案

I suggest that you use a Cloud Function that would create a new dedicated field when a Firestore doc is created. This field shall not be included in the object you pass to the set() method when you update the docs.

Here is a proposal for the Cloud Function code:

const functions = require('firebase-functions');

const admin = require('firebase-admin');

admin.initializeApp();


exports.productsCreatedDate = functions.firestore
  .document('products/{productId}')
  .onCreate((snap, context) => {

    return snap.ref.set(
      {
        calculatedCreatedAt: admin.firestore.FieldValue.serverTimestamp()
      },
      { merge: true }
    )
    .catch(error => {
       console.log(error);
       return false;
    });
  });


Based on Bob Snyder comment above, note that you could also do:

const docCreatedTimestamp = snap.createTime;
return snap.ref.set(
  {
    calculatedCreatedAt: docCreatedTimestamp
  },
  { merge: true }
)
.catch(...)

这篇关于在Firestore中管理createdAt时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆