如何使用pyrebase查询?

发布时间:2020-07-06 08:03

我正在学习如何在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中,我像“从AAA中选择*,其中D ='活动'并且(I ='USE002'或I ='USE003'或I ='USE004'或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()
回答1

Firebase实时数据库不允许查询equality is checked against a 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 and array-contains-any