Delete firebase data older than 2 hours

后端 未结 5 770
太阳男子
太阳男子 2020-11-22 03:17

I would like to delete data that is older than two hours. Currently, on the client-side, I loop through all the data and run a delete on the outdated data. When I do this, t

5条回答
  •  礼貌的吻别
    2020-11-22 03:52

    If someone will have the same problem, but in Firestore. I did a little script that at first read documents to console.log and then delete documents from a collection messages older than 24h. Using https://cron-job.org/en/ to refresh website every 24h and that's it. Code is below.

    var yesterday = firebase.firestore.Timestamp.now();
      yesterday.seconds = yesterday.seconds - (24 * 60 * 60);
      console.log("Test");
      db.collection("messages").where("date",">",yesterday)
          .get().then(function(querySnapshote) {
            querySnapshote.forEach(function(doc) {
              console.log(doc.id," => ",doc.data());
            });
          })
      .catch(function(error) {
            console.log("Error getting documents: ", error);
      });
    
      db.collection("messages").where("date","<",yesterday)
        .get().then(function(querySnapshote) {
          querySnapshote.forEach(element => {
            element.ref.delete();
          });
        })
    

提交回复
热议问题