简体   繁体   English

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. 我只想按顺序显示full_names 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)

The output is: 输出是:

alanisawesom
alanisawesome
alanisawesomee
alanisawesomeee
alanisawesomeeee

Where is my fault? 我的错在哪里? Can you fix it? 你能修好它吗?

EDIT: My Rules in Firebase is: 编辑:我在Firebase中的规则是:

{
  "rules": {
    "users": {
      ".indexOn": ["date_of_birth", "full_name"]
    },
    ".read": "true",
    ".write": "true"
  }
}

Your query tells the database to return the nodes under users ordered by their full_name property. 您的查询告诉数据库返回按其full_name属性排序的users下的节点。 But Firebase still returns the full nodes, not just the property you told it to order on. 但是Firebase仍然返回完整的节点,而不仅仅是返回您要求订购的属性。

To only display the full_name of each node, you need to get that property from each child node in the result. 要仅显示每个节点的full_name ,您需要从结果中的每个子节点获取该属性。 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? 我不是Pythonista,所以我在这里寻求语法方面的帮助: 如何使用python遍历每个firebase数据库子级?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM