Select last n records by key in firestore? - javascript

How does one select the last n items in a collection in firestore? I'm considering migrating a new project from the real time database to firestore. In the realtime database, I used the below code:
ref.orderByKey().limitToLast(n);
I've reviewed the docs at https://cloud.google.com/firestore/docs/query-data/order-limit-data, and they don't appear to mention how to achieve this in firestore. I suppose I could add a timestamp field to every element of the collection and then order by descending timestamp, but I think there should be an easier way to accomplish this.

In the Firebase Realtime Database there was the concept of a push ID, which was a key that was automatically chronologically ordered. This made ordering nodes chronologically in a query as easy as you've shown.
In Cloud Firestore there is no built-in metadata for when a document was added, or when it was last updated. If you want to build queried based on such metadata, you will have to set the corresponding properties on the documents yourself.

Related

How does deleting values in the Firebase Realtime Database work?

When we delete a child value in Firebase Realtime Database, it counts as an exclusion, correct?
But when we delete a node with several children, does it count as just an exclusion too? or is each child that belongs to the node counted as exclusion?
I'll explain it better: supposing it has this structure in firebase:
Each firebase plan has an exclusion limit.
1) How many exclusions are registered if I do this below? I believe that 3 exclusions, right?
referenceOne = (...).child("1");
referenceOne.removeValue();
referenceTwo = (...).child("2");
referenceTwo.removeValue();
referenceThree = (...).child("3");
referenceThree.removeValue();
2) Now, how many exclusions are registered if I do this?
reference = (...).child("users");
reference.removeValue();
3 exclusions or 1 exclusion?
You're mixing up Firebase database products. The documentation you linked to is for Firestore, but the code you're showing is working with Realtime Database. They have very different billing structures. What you linked to has no relation to what you're actually doing.
Realtime Database doesn't charge you for individual writes like Firestore does. If you want to know how Realtime Database bills you, read its documentation. You can delete as much data as you want without getting billed. You are billed primarily for data downloaded and data stored.

Why I have no results with a collection group query?

I'm using for first time collection groups querys on firestore in my web application but I can't retrive any data and I don't know why
I'm using Firebase SDK 7.6.1 and I have created the indexes correctly i think.
This is my Collection data structure:
And this are my Indexes:
And finally the code with I'm trying to retrieving data is:
doc = this.database.collectionGroup('Authors')
.where('Name','==',this.TextParams.trim().toLowerCase());
I expected to retrieve the documents where the author name is equal to my TextParams variable
In your screenshot, Authors is not a subcollection, it's an array type field. Since it's not a subcollection, a collection group query won't work at all. Also, it's currently not possible to search among array field items.
What you will have to do is actually put your Authors into a subcollection.

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.

How can I order the documents in Firestore?

How can I order the documents in Firestore? It looks like Firestore lists documents in alphabetic order by the ID I let Firestore automatically create. But I don't want that. I just want to see my newly added document added as the last document in the collection. How do I do that? Should I create my own alphabetic ID's?
Example of my collection in Firestore:
Firestore is meant to scale massively. At that scale, it doesn't matter what order the documents appear in the console, because the console is useless for viewing massive numbers of documents within a collection.
If you still need an ordering in the console, you will have to come up with document IDs of your own.
If you have a timestamp field, you can add a filter on timestamp and sort results by desc. Do not add a filter condition.

Firestore .orderBy() to also return documents without the field being queried

I updated a collection of documents with a new field that corresponds to a new feature. A rating out of 5. Previously I had a list all the documents in the order they were created in. Now I want to show all the documents in order of their rating. The problem with using .orderBy is that it only returns the newer documents with the rating field. Is there a way to return all documents, in order of rating, but to also include document that don't have ratings at the bottom?
All Firestore queries are optimized to return documents in a single range. Your requirement essentially become an OR condition, where it'd need to return documents from two ranges. This currently isn't possible in Firestore queries.

Categories