select method

Query<DocumentData> select([
  1. List<FieldPath> fieldPaths = const []
])

Creates and returns a new Query instance that applies a field mask to the result and returns only the specified subset of fields. You can specify a list of field paths to return, or use an empty list to only return the references of matching documents.

Queries that contain field masks cannot be listened to via onSnapshot() listeners.

This function returns a new (immutable) instance of the Query (rather than modify the existing instance) to impose the field mask.

  • fieldPaths The field paths to return.
final collectionRef = firestore.collection('col');
final documentRef = collectionRef.doc('doc');

return documentRef.set({x:10, y:5}).then(() {
  return collectionRef.where('x', '>', 5).select('y').get();
}).then((res) {
  print('y is ${res.docs[0].get('y')}.');
});

Implementation

Query<DocumentData> select([List<FieldPath> fieldPaths = const []]) {
  final fields = <firestore1.FieldReference>[
    if (fieldPaths.isEmpty)
      firestore1.FieldReference(
        fieldPath: FieldPath.documentId._formattedName,
      )
    else
      for (final fieldPath in fieldPaths)
        firestore1.FieldReference(fieldPath: fieldPath._formattedName),
  ];

  return Query<DocumentData>._(
    firestore: firestore,
    queryOptions: _queryOptions
        .copyWith(projection: firestore1.Projection(fields: fields))
        .withConverter(
          // By specifying a field mask, the query result no longer conforms to type
          // `T`. We there return `Query<DocumentData>`.
          _jsonConverter,
        ),
  );
}