首页 > 解决方案 > 如何使用pyrebase查询?

问题描述

我正在学习如何使用 python、django 和 pyrebase 进行查询。查询多个键值时出现问题。

例如:

这是我的数据结构:

    {
    "root": {
        "account": {
            "ACC0001": {
                "id": "ACC0001",
                "create_day": "2020-04-20 16:56:11",
                "create_by": "USE001",
                "brief_name": "AAAAA",
                "status": "active"
            },
            "ACC0002": {
                "id": "ACC0002",
                "create_day": "2020-04-20 16:56:12",
                "create_by": "USE002",
                "brief_name": "BBBBB",
                "status": "inactive"
            },
            "ACC0003": {
                "id": "ACC0003",
                "create_day": "2020-04-20 16:56:13",
                "create_by": "USE003",
                "brief_name": "CCCCC",
                "status": "active"
            },
            "ACC0004": {
                "id": "ACC0004",
                "create_day": "2020-04-20 16:56:14",
                "create_by": "USE004",
                "brief_name": "DDDDD",
                "status": "inactive"
            },
            "ACC0005": {
                "id": "ACC0005",
                "create_day": "2020-04-20 16:56:15",
                "create_by": "USE005",
                "brief_name": "EEEEE",
                "status": "inactive"
            },
            
            ......

            "ACC9999": {
                "id": "ACC9999",
                "create_day": "2020-04-20 16:56:15",
                "create_by": "USE100",
                "brief_name": "FFFFF",
                "status": "active"
            }
        }
    }
}

在 SQL 中,我使用类似“select * from AAA where D = 'active' and (I = 'USE002' or I = 'USE003' or I = 'USE004' or I = 'USE005')” 我如何在 python 中做到这一点, django 和 pyrebase 获取列表用户的记录?我只得到一个用户。我的代码如下:

    config = {
    'apiKey': "xxxxxx",
    'authDomain': "xxxx.firebaseapp.com",
    'databaseURL': "https://xxxx.firebaseio.com",
    'projectId': "xxxx-92dec",
    'storageBucket': "xxxx-92dec.appspot.com",
    'messagingSenderId': "xxxx598699",
    'appId': "1:xxxxx8699:web:xxxxx5a9e2920ec32e",
    'measurementId': "xxxx360FN"
    }

firebase = pyrebase.initialize_app(config)
database = firebase.database()
// how can get all record of list create_by ['USE001','USE002','USE003'...]
objuser = database.child('account').order_by_child('create_by').equal_to('USE001').get().val()

标签: pythondjangofirebasepyrebase

解决方案


Firebase 实时数据库不允许查询在哪里检查相等性的 list

实现最终目标的两种方法:

为每个用户使用真值。将数据库更改为以下内容:

{
    "root": {
        "account": {
            "ACC0001": {
                "id": "ACC0001",
                "create_day": "2020-04-20 16:56:11",
                "create_by": "USE001",
                "create_by_USE001": true,
                "create_by_USE002": false,
                "create_by_USE003": false,
                "create_by_USE004": false,
                "brief_name": "AAAAA",
                "status": "active"
            },
            "ACC0002": {
                "id": "ACC0002",
                "create_day": "2020-04-20 16:56:12",
                "create_by": "USE002",
                "create_by_USE001": false,
                "create_by_USE002": true,
                "create_by_USE003": false,
                "create_by_USE004": false,
                "brief_name": "BBBBB",
                "status": "inactive"
            },
  ...

更好的方法是使用 firestore,因为它支持in 和 array-contains-any


推荐阅读