Reputation: 3112
I have around 2 million users at a branch in a database. Each user may contain a property called activeSessionId
which tells me if this user has ever started a web session to my app. There is also an array called sessions that contains data for all the web sessions this user has ever started. I have found some bugs in the code which may cause the users who have started a web session to the app to have some invalid data causing the mobile app to become unresponsive. I want to traverse all the users who have started a web session and fix that data.
If I traverse over all the users, the code fails with firebase complaining about too much data to read. So, I am trying to think of a query that could just give me the users that have an activeSessionId
defined and I just traverse over those. But I can't think of any such query. The equalTo
method doesn't allow for an undefined
value for a property.
Below is what a user looks like in firebase that has an activeSessionId
.
Top level
userInfos: {
"-NL1sdfee29E7bQ53_rJTW": {
"activeSessionId": "-NL1aKF29E7bQ53_rJTW",
"alarmsCount": {
"ownAlarms": {
"Personal": {
"edit": 1244,
"new": 436
}
}
},
"contactsLastUpdatedAt": 1675435956007,
"deviceLocale": "en-US",
"deviceOs": "android",
"joinDate": 1649084020051,
"lastActiveAt": 1675435953311,
"maxAllowedAlarms": 5,
"rateTheApp": {
"done": true,
"lastAsked": 1675435981073
},
"release": "7.4.3",
"sessions": {
"-NL1aKF29E7bQ53_rJTW": {
"browser": "Chrome",
"id": "-NL1aKF29E7bQ53_rJTW",
"lastActive": "10 Jan 2023, 11:22 AM",
"platform": "Windows"
}
},
"timezone": "America/Denver",
"timezoneOffset": 420,
"totalNumberOfContacts": 282
}
}
Is it possible to create such a query for Firebase Realtime Database?
Upvotes: 0
Views: 68
Reputation: 138824
In the Realtime Database, there is no way you can query a node based on a field that doesn't exist. So if you want to perform a query that returns the users that have the activeSessionId
field set to a particular value and the users that don't have the activeSessionId
field at all, that isn't possible.
A possible workaround would be to denormalize the data and create a node that contains only the users that have already set the activeSessionId
field or don't have the activeSessionId
field at all.
However, if you consider at some point in time using Cloud Firestore, then please note that you can achieve something similar using whereIn() in a query that looks like this:
db.collection("products").whereIn("activeSessionId", Arrays.asList("someId", "defaultId"))
This means that you have to set a default value to all users who don't have set a specific ID.
Also, in the case of Firestore, a query like this:
db.collection("products").whereIn("activeSessionId", Arrays.asList("someId", null));
Won't work. Besides that, you cannot also query for non-existing fields. There is no "undefined" value for Firestore fields. Here are the supported data types. You can see that null is a supported data type but not undefined.
So we cannot query for fields that don't exist in Firestore. We can only search for fields that exist in the index, hence the presence of the defaultId
in that array.
Upvotes: 1