0

I was wondering if it's possible to make a snapshot of all documents in a collection. But this snapshot (or get) should include only certain items of the documents.

I.e.: I have a collection of 'users', each document has items 'name', 'age' and 'score'. From this I want to have a snapshot that only includes the data of 'name'.

Thanks for help!

wenzf
  • 337
  • 6
  • 14
  • The client-side SDKs of Firestore always read and return full documents. The Server/Admin SDK and REST API can do so-called projections. The most common approach is to create (additional) documents that contain only the data you need, so for example a collection `user_profiles` with just their `name`, `age`, and `score`. – Frank van Puffelen Mar 30 '19 at 14:37

1 Answers1

1

There is no way of querying for just parts of a document from Firestore, you have to return the entire thing. That is just have Firestore works.

(You can of course filter out the elements you don't want on your client after you have retrieved the document)

Edit:

I might have been a little brash here. This is true from a Web client enviroment. I am not a 100% sure if you are using the Admin SDK from a node environment.

SnorreDan
  • 2,740
  • 1
  • 14
  • 17
  • Thanks for the clear and quick reply! Just wondering... would there be any difference with the 'realtime-database'? – wenzf Mar 30 '19 at 08:11
  • 1
    The Realtime Database is just a big Javascript object, if I remember correctly it returns everything that is sublevel from the point you enter the object. So in general you will probably end up with returning more data than necessary from the Realtime Database as well. – SnorreDan Mar 30 '19 at 08:18
  • I think in my case the solution is to store the data on entry in several collections and bind the items of the several collections with UIDs - instead of storing all data in one collection. Thanks again! – wenzf Mar 30 '19 at 08:38
  • Yes that is probably a good approach – SnorreDan Mar 30 '19 at 09:18
  • @SnorreDan The Admin SDK can indeed get a so-called projection of each document. See Gil's answer here: https://stackoverflow.com/questions/47061903/how-to-get-a-list-of-keys-in-firestore – Frank van Puffelen Mar 30 '19 at 14:34