skip to Main Content

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


  1. I don’t know if this is exactly what you need but firebase docs has below example:Order and limit data

    import { query, where, orderBy, limit } from "firebase/firestore";  
    
    const q = query(citiesRef, where("population", ">", 100000), orderBy("population"), limit(2));
    

    if you adjust where part to your id, then sort by date it should work.

    Login or Signup to reply.
  2. Suppose I know a document ID, is it possible to get the collection documents from that certain document ID?

    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.

    I’m not sure if Firestore documents are ordered by date.

    No, there is no time component inside the document IDs.

    I am trying to get the most recent documents from a certain document ID.

    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() or starAfter() if you need that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search