0

Imagine that I have a collection of documents:

collection: 'products'

And inside that collection I have documents, and each document has a really heavy field that I want filtered before it gets sent to my clients.

document product1:

{
  id: 'someUniqueKey',
  title: 'This is the title',
  price: 35.00,
  heavyField: [
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'longStringxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}

Can I query for those documents inside the products collection and leave this specific heavyField out of my response?

I would like to be able to get the documents with 1 of their fields filtered out. Maybe select the fields I want to receive on my client.

Is this possible? Or in this case it's best to structure my data different and leave the heavyField in a different collection?

cbdeveloper
  • 27,898
  • 37
  • 155
  • 336

1 Answers1

1

In SQL, this would be called a "projection" in a query. Cloud Firestore doesn't support projections from web and mobile client SDKs. You should instead put your "heavy" fields into documents in another collection, and query that only as needed.

If you're wondering why, it partly has to do with the way that the client local persistence layer works. It would make caching much more complicated if only certain fields existed locally for a given document. It's much more straightforward to simply fetch and store the entire document, so there is no question whether or not the document is locally available.

For the purpose of data modeling, it's best to think of documents as "atomic units", all or nothing, that can't be broken down.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441