In the last section, we have seen how to work with remote objects, but: What about doing queries to the database?

Sometimes our app’s workflow will need to check some data, for example:

Database: give me all chat rooms where the logged user appears

And then print all these chats in a list.

Let’s see how simple is querying data.

Imagine we have a database called myDatabase where we store chat rooms.

{
    "chats": {
        "123456789ASDF": {
            "id": "123456789ASDF",
            "members": {
                "EdfRtgyhBvfDc": {
                    "id": "EdfRtgyhBvfDc"
                    "rol": "admin"
                }
            }
            "messages": {
                "0": {
                    "message": "Holi 🤭"
                    "author": "EdfRtgyhBvfDc"
                }
            }
        }
    }
}
Database.query("myDatabase",
       "/chats/*",
       "{\"members\": { \"" + 
            user.getId() + "\": { \"id\": \"" + user.getId() +
       "\" } } }",
       "{ \"id\": \"\" }",
        new QueryCallback() {
    
            @Override
            public void response(List<LinkedTreeMap<String, String>> list) {
                for(LinkedTreeMap m : list) {
                    String id = (String) m.get("id");
                    Log.d("ROTOR", "chat found: " + id);
                    // TODO load chat on list
                }
            }

        }
    );
);

What are we seeing here?

The first parameter is the database name where we will look for data: myDatabase

The second parameter is the path where data should exist: /chats/*

The third parameter is the query:

{
    "members": {
        "EdfRtgyhBvfDc": {
            "id": "EdfRtgyhBvfDc"
        }
    }
}

The fourth parameter is the mask, which will refactor the response:

{
    "id": ""
}

Result

Query method will always return a list of results. In this case (with a mask) the response will be:

[
    {
        "id": "123456789ASDF"
    }
]

If we don’t define a mask, the result will be:

[
    {
        "id": "123456789ASDF",
        "members": {
            "EdfRtgyhBvfDc": {
                "id": "EdfRtgyhBvfDc"
                "rol": "admin"
            }
        }
        "messages": {
            "0": {
                "message": "Holi 🤭"
                "author": "EdfRtgyhBvfDc"
            }
        }
    }
]