I’m getting a collection from my Firestore database and adding the document values to an array in JS:
var data = myCollection();
var myArray = [];
data.forEach(function(data) {
var splitPath = data.name.split('/');
var documentId = splitPath[splitPath.length - 1];
var name = data.fields.name ? data.fields.name.stringValue : '';
var country = data.fields.vin ? data.fields.vin.stringValue : '';
myArray.push( [ documentId, name, country ] );
});
Suppose I know a document ID, is it possible to get the collection documents from that certain document ID?
I’m not sure if Firestore documents are ordered by date. I am trying to get the most recent documents from a certain document ID.
2
Answers
I don’t know if this is exactly what you need but firebase docs has below example:Order and limit data
if you adjust where part to your id, then sort by date it should work.
When it comes to the Firebase console, all documents are sorted lexicographically by ID and this behavior can’t be changed. When it comes to code, it’s up to you to choose how to order the results.
No, there is no time component inside the document IDs.
In that case, the simplest solution would be to add a timestamp field in each document and order them according to that field.
However, Firebase Realtime Database pushed IDs do contain a time component. So if you want, you can add that data there. Both databases are working great together.
If you have multiple documents and you want to implement pagination, for example, you can use query cursors and use
startAt()
orstarAfter()
if you need that.