How to filter data in Firebase and Ionic? - javascript

I use Ionic and Firebase in my project.
I need to filter users by gender and, due to there is too much data, I also have to implement pagination for ease of navigating through.
I have been dealing with this problem for a long time, but still could not find a valid solution.
My database structure is:
My code is:
ref = this.afDB.database.ref('Users').orderByChild('Gender').equalTo('male').orderByChild('Id').startAt(lastItemKey).limitToFirst(10)

You can't call orderBy...() multiple times on the same query. See Query based on multiple where clauses in Firebase
But you can use the two-parameter version of equalTo() to accomplish what you want:
db.ref('Users').orderByChild('Gender').equalTo('male', lastItemKey).limitToFirst(10)
The lastItemKey in this snippet is used when there are multiple child nodes with Gender equal to male to determine at which one to start.

Related

Auto generate sub database firestore

I have a firestore collection with a bunch of documents, each with plenty subfields. On a web page I need a list of a specific subfields from each document.
Currently I load the the entire database when you load the page and then loop through and get the wanted values. This uses way to many reads to get very little data.
Is there a way to solve this e.g. a autogenerated a collection that contains field from other collection in an array or something.
Many thanks in advance
Auto-creating such a subcollection with just the fields you need is a great way to reduce the bandwidth needed to load the data.
There is nothing built into Firestore to create those derived documents, but it's fairly easy to build something using Cloud Functions. Create a function that responds to a Firestore onWrite trigger, and write the subset of the data to its destination there. It's common to have a separate Cloud Function for each such use-case, and I regularly see projects with 100+ such functions.
I expect we'll also start seeing Firebase Extensions for this type of thing, but right now no-one seems to have built one.

How to get firebase data in inner node without the outer node name?

I want to read data from firebase, in the ref ("locations/{{someLocation}}/logs/{{someDevice}}")
but I do not know if exists a correct form to do, because I need to use ".on" to hear constantly all the devices changes, the problem is that I need to read all devices in logs in all locations, but I do not want another information, I just need information in logs or in the specific device, I know that I could reach that invoking one callback ".on" for each device, but I want a cleaner form to make it and in the documentation, there is no help for this.
maybe I could invoke a method like that
firebase.database("locations/{eachLocation}/logs/{eachDevice}").ref().on...
because I do not want all the devices, the problem is that I do not know if there is a form to make it thank you.
It sounds like you've nested your data too much. The Firebase documentation has explicit sections on avoiding nesting data and flattening data structures with hints on how to prevent this.
At first glance you'll need at least two top-level lists: locations and locationLogs. Under each you have the same keys as you have now, but the logs are now under /locationLogs/$key instead of under /locations/$key/logs. With that change you can get the logs for a location without getting the other data for that location.
If you don't know the location key, but don't want to get the logs for all locations, it sounds like you've nested another level too deep. Firebase queries work on a flat list of nodes, and can't search across multiple levels of unknown keys. If you want to search across all logs across all locations, you will need to keep a flat list of all logs. You can then tie each log back to its location, by adding the location ID to each log.
So that might lead to a structure of:
logs: {
"adjustableLight....1": {
locationId: "DTZB35",
date: 156...,"
...
}
}
What you're trying to do isn't supported by Realtime Database. There are no wildcard queries or placeholders. You must be able to build the full path to the node whose data you want to get.
Consider changing the structure of your data so that you can more easily find the nodes you want. It is common in nosql type databases to duplicate data into structures that are easier to query for a particular use case.

Firebase database retrieving data from comma seperated list

I want to store the comma separated ids on a child node & how can I filter data as in sql we can use IN clause to fetch data any possibility in firebase to perform this kind of operation in firebase database.
Please suggest any possible solution for this.
Firebase Realtime Database doesn't have the equivalent of SQLs IN clause. It also doesn't have a way to find a substring in a value. So the data model you are looking to use, doesn't allow the use-case you want. As usual with NoSQL databases, the solution is to pick a data model that does allow your use-case..
The most likely cause I know for the structure you describe is to associate the child node with a bunch of categories. If that is your case, read my answer here for a proper data structure: Firebase query if child of child contains a value
This is one of the cases where the new Cloud Firestore database offers better querying support, since it recently added a feature to efficiently test if an array contains a certain value (video). If you're only just getting started with your project, you might want to check if Firestore is a better fit for your use-cases.

Firestore and bloom filters to avoid repetition

I am creating a tinder style app in which users are shown to one another and either liked or disliked.
I am using Firestore from firebase along with ionic 3 to achieve this. so far Firestore has been perfect and I have been able to do everything required using it.
However now I am at the stage where I need to ensure a user is not displayed to another user after they have been swiped once.
Cureently i query Firestore as follows:
this.firestore.col$('Users', ref =>
ref.where(interests.<INTEREST CHOSEN BY USER>, '>', 0)
.limit(30))
This returns a list of users with interests the same as the user who is swiping and i want to ensure once they have been swiped once, that user is never shown again.
I tried using the field createdBy to achieve this, by saving the most recent date, and then querying for results that have a createAt date, after this, however Firestore does not allow comparisons on more than one field in one query.
Bloom filters seem to be the way to achieve what i need. I am wondering the best way to approach this.
Option 1:
Save a bloom filter into a users record and compare query results to this when they are downloaded, and filter out using map function.
I have not used bloom filters before, and am not sure how to best approach this when using firestore.
Is there a better approach to take?

How to query in couchdb with multiple key combinations using javascript without writing separate view for each combination?

I am trying to fetch documents from couchdb based on certain specific filters through javascript. For example i need to get the list of employees from a db where the key can be either city, age, state, gross income, gender or a combination of two or more such keys.
The problem i am facing is as the number of possible keys increase the number of views i need to write also increases drastically. I want to avoid writing so many views. So is it possible to do so ??
In addition to checking out Matt's suggestion about couchdb-lucene, you might also look into list functions: they're quite useful when you have a small set of basic view queries that will reduce the number of records fetched to a manageable level and you want to do a bunch of ad-hoc queries that further filter those records.

Categories