只留下NN最新的条目,并删除所有其余的 [英] Leave only NN newest entries and delete all the rest

查看:181
本文介绍了只留下NN最新的条目,并删除所有其余的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构。
每个用户都有自己的数据。

  --- + root_child 
|
+ --- + Gy7FXRbRjDfAKWu7a95NgiGIZUk1(Firebase用户标识)
|
|
+ --- + KlNlb71qtQUXIGA4cNa(随机密钥,由Firebase生成)
| |
| + --- timestamp = 1234567890
| |
| + ---(其他数据字段...)
|
|
+ --- + KlNlcmfMTDjxQ0BwW1K
| |
| + ---时间戳= 9876543211
| |
| + ---(其他数据字段...)
|
|
+ --- +(...)

添加记录的方式如下:

  databaseReference = FirebaseDatabase.getInstance()。getReference(root_child); 
databaseReference.child(firebaseUser.getUid())
.push()
.setValue(val);




push() ...在单个客户端上生成的位置将被排序
创建它们的顺序...

现在,我怎么能只留下100个最新的条目(对于指定的用户ID),并删除所有其余的?



伪代码:

  databaseReference = FirebaseDatabase.getInstance()
.getReference(root_child)
.child(firebaseUser.getUid())
.deleteLastNnRecords();


解决方案

我可能会使用排序和过滤功能

首先,我将使用一个值监听器来获取孩子数: https ://stackoverflow.com/a/43607203/7949​​696 ,或者在云功能的情况下,我会从写入触发器上的 snap 如果childCount> 100,我会在时间戳上 OrderByChild() ,然后按 LimitToLast(childCount - 100)



过滤如果我是你,我会使用云功能来实现这一点,并设置一个函数写入 root_child / $ {uid} /






编辑: :
https://github.com/firebase/functions -samples / tree / master / limit-children



干杯!





$编辑2:看起来像 push()按时间顺序排序,所以不需要 OrderByChild


I have the following structure. Each user has his own data.

---+ root_child
   |
   +---+ Gy7FXRbRjDfAKWu7a95NgiGIZUk1  (Firebase User Id)
       |
       |
       +---+ KlNlb71qtQUXIGA4cNa (random key, generated by Firebase)
       |   |
       |   +--- timestamp = 1234567890
       |   |
       |   +--- (other data field ...)
       |
       |
       +---+ KlNlcmfMTDjxQ0BwW1K
       |   |
       |   +--- timestamp = 9876543211
       |   |
       |   +--- (other data field ...)
       |
       |
       +---+ (...)

Adding records occurs in this way:

databaseReference = FirebaseDatabase.getInstance().getReference("root_child");
databaseReference.child(firebaseUser.getUid())
    .push()
    .setValue(val);

push() ... Locations generated on a single client will be sorted in the order that they are created...

Now, how can I leave only 100 newest entries (for the specified user id) and delete all the rest?

Pseudocode :

databaseReference = FirebaseDatabase.getInstance()
    .getReference("root_child")
    .child(firebaseUser.getUid())
    .deleteLastNnRecords();

解决方案

I would probably use the sort and filter functions.

First, I would grab the children count with a single value listener: https://stackoverflow.com/a/43607203/7949696 , Or in the case of a cloud function I would count the children from the snap on the write trigger.

Then, if childCount > 100, I would OrderByChild() on your timestamp and then filter by LimitToLast(childCount - 100)

If I were you, I would use cloud functions to achieve this and set a function on write to root_child/${uid}/


Edit: Source code link from comments: https://github.com/firebase/functions-samples/tree/master/limit-children

Cheers!


Edit 2: Looks like push() is chronologically sorted, so no need to OrderByChild

这篇关于只留下NN最新的条目,并删除所有其余的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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