Python-Google Firebase中的复杂查询 [英] Python - Complex Query in Google Firebase
问题描述
我有一个如下所示的数据库
I have a database which is shown in below
我只想按顺序显示全名
.我想要该输出:
I want to show only the full_names
in order. I want that output:
Alan Turing
Alan Turing
Alan Turing
Alan Turing
Alan Turing
我在下面尝试了此代码,但显示了用户名.
I tried with this code below but it shows the user names.
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
# Fetch the service account key JSON file contents
cred = credentials.Certificate('BLABLA.json')
# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://BLABLA.firebaseio.com/'
})
ref = db.reference('users')
snapshot = ref.order_by_child('full_name').get()
for key in snapshot:
print(key)
输出为:
alanisawesom
alanisawesome
alanisawesomee
alanisawesomeee
alanisawesomeeee
我的错在哪里?你能解决吗?
Where is my fault? Can you fix it?
我在Firebase中的规则是:
{
"rules": {
"users": {
".indexOn": ["date_of_birth", "full_name"]
},
".read": "true",
".write": "true"
}
}
推荐答案
您的查询告诉数据库返回 users
下按其 full_name
属性排序的节点.但是Firebase仍会返回完整的节点,而不仅仅是返回您要求订购的属性.
Your query tells the database to return the nodes under users
ordered by their full_name
property. But Firebase still returns the full nodes, not just the property you told it to order on.
要仅显示每个节点的 full_name
,您需要从结果中的每个子节点获取该属性.像这样:
To only display the full_name
of each node, you need to get that property from each child node in the result. Something like this:
ref = db.reference('users')
snapshot = ref.order_by_child('full_name').get()
for user in snapshot.each():
print(user.child("full_name").val())
I'm not a Pythonista, so I looked here for help on syntax: How to loop through each firebase database child with python?
这篇关于Python-Google Firebase中的复杂查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!