如何从 Firebase 实时数据库获取最新记录
How can I get the latest record from Firebase Realtime Database
我正在使用 Pyrebase 从实时数据库接收数据。其实我可以直接接收数据,但我只需要最新的记录。
这是实时数据库:
假设最大的键(这里是4
)是最近的记录,这里是如何用pyrebase
直接检索它:
firebase = pyrebase.initialize_app(config)
db = firebase.database()
last_record = db.child('input').order_by_key().limit_to_last(1).get().val()
print(last_record)
# should print OrderedDict([('4', {'input1': ..., 'input2': ..., 'input3': ...})])
order_by_key
orders in ascending order by default 和 limit_to_last(1)
确保您只检索一条记录,即查询中的最后一条记录。
However there is a bug for calling order_by
in the pyrebase
library. Fortunately a fork of it, pyrebase4
,它已修复所以确保使用这个:
$ pip uninstall pyrebase && pip install pyrebase4
我正在使用 Pyrebase 从实时数据库接收数据。其实我可以直接接收数据,但我只需要最新的记录。
这是实时数据库:
假设最大的键(这里是4
)是最近的记录,这里是如何用pyrebase
直接检索它:
firebase = pyrebase.initialize_app(config)
db = firebase.database()
last_record = db.child('input').order_by_key().limit_to_last(1).get().val()
print(last_record)
# should print OrderedDict([('4', {'input1': ..., 'input2': ..., 'input3': ...})])
order_by_key
orders in ascending order by default 和 limit_to_last(1)
确保您只检索一条记录,即查询中的最后一条记录。
However there is a bug for calling order_by
in the pyrebase
library. Fortunately a fork of it, pyrebase4
,它已修复所以确保使用这个:
$ pip uninstall pyrebase && pip install pyrebase4