Home > Blockchain >  How to get a subset of object's properties when querying an entire collection in Firebase Fires
How to get a subset of object's properties when querying an entire collection in Firebase Fires

Time:09-27

I have this Firestore onSnapshot listener on a collection (getting the entire collection). I would like to get only a subset of the the properties of each object.

Something like we do with the firebase-admin using select() on a query:

Ex: admin.firestore().collection('NAME').where(conditions).select('field1', 'field2').get()

This is the onSnapshot code: it works just fine but it's getting the full objects (containing all the properties).

const db = getFirestore();
const col = 'COL_NAME';
const q = query(collection(db, col));
onSnapshot(q, (querySnapshot) => { // How to get only a subset of fields here ?
  const results = {};
  querySnapshot.forEach((doc) => {
    // Do something with each object
  });
});

Of course I can map it on the client, but my goal is to keep data network traffic to a minimum.

How can I do it?

CodePudding user response:

With the Client SDKs this is not possible.

As you have mentioned this is possible with the Admin SDK but it is also possible with the Firestore REST API: You can use a DocumentMask when fetching documents, which will "restrict a get operation on a document to a subset of its fields. ".

Note however that fetching via the REST API from a web app is much less simple than using the JS SDK. In particular the format of the response is a complex object that is not funny to parse...


Another approach would be to dernomalize your data: You create another collection which contains documents that only have the fields you want to display in the front end.

The complexity is that you need to keep the two collections in sync: when you create/modify/delete a doc in the master collection, you need to update the other collection. This can be done from your front-end (e.g. you write to the two collections in a batched write) or with a Cloud Function, triggered in the back-end with an onWrite trigger.

  • Related