skip to Main Content

After reading a bunch of threads regarding trying to perform queries on fields with dynamic keys in a document, I have two questions.

  1. Is it possible to perform the following "query" on my data model: "Get field where createdDate is equal or greater than todays date"?
  2. Should I change the data model?

I have documents (test.com in the below image) that have fields where the key is dynamic (0, 1, 2, 3 and so on). Each field is a "map", which (correct me if Im wrong) contains an object. Each object have a "createdAt". I want to query all fields on "createdAt" and return all fields where "createdAt" is equal to or greater than todays date. So in my image, as of today it is the 12 of january, the field with the key "1" should be returned.

My query below doesn’t work due to I don’t "reach down to each field", would each document have just color, createdAt and domainName flattened It would have worked.

  const domainsQuery = query(
    collectionGroup(db, "domains"),
    where("releaseAt", ">=", new Date())
  );

My data

2

Answers


  1. It’s not possible to fetch only some fields of a document in firebase. You always fetch all fields of a document. You can filter returned documents but not the fields of documents. You should probably adjust your model.

    I recommend just creating an additional subcollection (name can be arbitrary) and making those dynamic keys into ids of subdocuments instead.

    Login or Signup to reply.
  2. Is it possible to perform the following "query" on my data model: "Get field where createdDate is equal or greater than today’s date"?

    No, there is not. You cannot filter documents based on a single field property that exists inside an object, which is contained in an array. What you are looking for cannot be achieved using partial data.

    If you need to filter based only on a particular field, then you should consider duplicating the data on which you want to perform the filtering and adding it to a separate array. In this way, you can query the collection using the array-contains operator.

    Should I change the data model?

    A possible solution would be to get all those objects out of the array and add them as separate documents inside a sub-collection. In that way, you can simply perform the desired query.

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