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.
Related
I want to move a specific doc with all his nested collections from one collection to another.
is it possible?
db.collection('codes).doc(specificDoc).setLocation(db.collection('archive))
or somthing like this
Firestore does not offer any way to move documents between collections. What you will need to do instead is query for the documents to move, iterate them, write new documents in the archive collection, and delete the original.
Consider instead just using a field in each document to mark whether or not it is archived, and use that field to filter the results.
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.
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.
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.
I want to get the last document in een documentdb collection. But i have 2 problems.
First problem is when i create documents in a loop, it does not create documents in same order and therefore the last document in the database is not the last element in the array. I fixed that with slice() function. I take out the last element of the array and create a document apart with setTimeout() function. It works but is there any better way for this?
Second problem is querying in database. I do not want to query all documents in the database. Therefore i want to use LAST() SQL function, but it gives error 'LAST' is not a recognized built-in function name.. I can use this statement well.
SELECT TOP 1 CustomerName FROM Customers
ORDER BY CustomerID DESC;
But is that useful to query all documents? I mean if there are 10.000 documents or more is that a good way? Or is there any way to get the last document without querying all documents?
First: There's really no concept of a last document. If you add an ORDER BY on your query, then you can fetch the TOP N as you posted in your 2nd example. Without a sort order, there's no concept of "last" (or even "first" for that matter - TOP 1 on an unordered query could theoretically return any document - you'd just skim off the first one in the result set).
As for querying all documents: You're not querying all documents. Since, by default, all properties are indexed, you're just getting the first document based on the CustomerID property sort order. There's no reason for the database engine to do a collection-scan for this type of query.