3

I'm learning how to use queries with python, django and pyrebase. I have a problem when querying multiple values of key.

For example:

This is my data structure:

    {
    "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"
            }
        }
    }
}

in SQL i use like "select * from AAA where D = 'active' and (I = 'USE002' or I = 'USE003' or I = 'USE004' or I = 'USE005')" How can i do it in python, django and pyrebase get record of list user ? I just get exactly only one user. My code is below :

    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()
Naoa Lee
  • 135
  • 2
  • 19
  • Can you clarify what your intended output is? Are you looking to create a list of all "create_by" or what are you trying to filter to? – MyNameIsCaleb Jul 08 '20 at 16:57
  • Thanks @MyNameIsCaleb. I have a user list ['USE001','USE002','USE003'...] and I want to get all the record of the users with "create_by" in that list. In SQL i use "select * from AAA where D = 'active' and (I = 'USE002' or I = 'USE003' or I = 'USE004' or I = 'USE005')" but in pyrebase i don't know way query ? help me !! show me how to do it plzzzzzz – Naoa Lee Jul 09 '20 at 03:21

1 Answers1

1

Firebase Realtime Database doesn't allow querying where equality is checked against a list.

Two ways to achieve your end goal:

Use truth values for each user. Changing database to something like this:

{
    "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"
            },
  ...

A better approach would be using firestore as it support in and array-contains-any

frunkad
  • 2,433
  • 1
  • 23
  • 35
  • Thanks @frunkad for answering my questions ! How to use this case example? I want get all record have "status" = "inactive" and this record of USE002,USE003,USE004. I don't know how to use it yet – Naoa Lee Jul 12 '20 at 18:39
  • The above approach (the sample mentioned in answer) or in firestore? – frunkad Jul 13 '20 at 15:06