Search code examples
node.jsgoogle-cloud-firestorefirebase-admin

How can I select specific fields using the firestore modular api


In the <= V8 Firestore API I could query a set of documents and extract a subset of fields with code like the following:

  const membersRef = db.collection('members');
  const snapshot = await membersRef.select('First', 'Last').get();

With the new modular api I can do the following:

import { collection } from 'firebase/firestore';
const membersRef = collection(db,'members');
// how to select only First and Last from members ?

I can't find a select() function to specify fields in the new api. How can this be done using the V9 modular api?

I know I can read the entire collection and use .map()/.forEach to reduce the fields but I have some large fields I would rather not query unless I need them for a specific document. The use of select to have the server pull out only the fields needed is useful.


Solution

  • The client-side SDKs for Firebase don't have a select method/function, but always retrieve full documents. So the select method didn't exist in the v8 CollectionReference or Query classes, nor does it exist as a top-level function in the v9 modular SDK.

    There is a select() method in the Admin SDK for Node.js, but that one still works as a method on CollectionRefence as far as I can tell.