gpt4 book ai didi

Python firebase数据库浅层获取数据

转载 作者:行者123 更新时间:2023-12-01 01:30:58 30 4
gpt4 key购买 nike

众所周知,Firebase 实时数据库 is one big JSON tree :

Because the Firebase Realtime Database allows nesting data up to 32 levels deep, you might be tempted to think that this should be the default structure. However, when you fetch data at a location in your database, you also retrieve all of its child nodes.

现在我需要进入的数据库具有以下结构:

root
|
|- category 1
| |
| |
| |-sub category 1
| | |
| | |- record 1
| | | |- (...)
| | |
| | |- record 2
| | | |- (...)
| | |
| | |- (...) record n
| |
| |
| |- (...) sub category n
|
| - (...) category n

每个类别都有数千个子类别,每个子类别有数千条记录。

有没有办法使用python和官方迭代实时数据库firebase-admin python SDK并仅获取数据库的浅副本(也称为键)以获取某些行?

我想避免告诉数据库架构师重新建模数据库以将记录数据非规范化到另一个节点

现在我只找到了使用官方 SDK 获取 key 及其中所有相应数据的方法:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

# Fetch the service account key JSON file contents
cred = credentials.Certificate('firebase-adminsdk.json')
# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://<app_name>.firebaseio.com/'
})

ref = db.reference('category 1')
print(ref.get())

最佳答案

Python Admin SDK 支持对引用进行浅读。

print(ref.get(shallow=True))

请参阅文档:https://firebase.google.com/docs/reference/admin/python/firebase_admin.db#reference

关于Python firebase数据库浅层获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52871212/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com