Class CollectionGroup (7.6.0)

A CollectionGroup refers to all documents that are contained in a collection or subcollection with a specific collection ID.

CollectionGroup

Inheritance

Query<AppModelType, DbModelType> > CollectionGroup

Package

@google-cloud/firestore

Constructors

(constructor)(firestore, collectionId, converter)

constructor(firestore: Firestore, collectionId: string, converter: firestore.FirestoreDataConverter<AppModelType, DbModelType> | undefined);

Constructs a new instance of the CollectionGroup class

Parameters
NameDescription
firestore Firestore
collectionId string
converter FirebaseFirestore.FirestoreDataConverter<AppModelType, DbModelType> | undefined

Methods

getPartitions(desiredPartitionCount)

getPartitions(desiredPartitionCount: number): AsyncIterable<QueryPartition<AppModelType, DbModelType>>;

Partitions a query by returning partition cursors that can be used to run the query in parallel. The returned cursors are split points that can be used as starting and end points for individual query invocations.

Parameter
NameDescription
desiredPartitionCount number

The desired maximum number of partition points. The number must be strictly positive. The actual number of partitions returned may be fewer. {AsyncIterable

Returns
TypeDescription
AsyncIterable<QueryPartition<AppModelType, DbModelType>>
Example

const query = firestore.collectionGroup('collectionId');
for await (const partition of query.getPartitions(42)) {
  const partitionedQuery = partition.toQuery();
  const querySnapshot = await partitionedQuery.get();
  console.log(`Partition contained ${querySnapshot.length} documents`);
}

withConverter(converter)

withConverter(converter: null): CollectionGroup;

Applies a custom data converter to this CollectionGroup, allowing you to use your own custom model objects with Firestore. When you call get() on the returned CollectionGroup, the provided converter will convert between Firestore data of type NewDbModelType and your custom type NewAppModelType.

Using the converter allows you to specify generic type arguments when storing and retrieving objects from Firestore.

Passing in null as the converter parameter removes the current converter.

Parameter
NameDescription
converter null

Converts objects to and from Firestore. Passing in null removes the current converter. {CollectionGroup} A CollectionGroup that uses the provided converter.

Returns
TypeDescription
CollectionGroup
Example

class Post {
  constructor(readonly title: string, readonly author: string) {}

  toString(): string {
    return this.title + ', by ' + this.author;
  }
}

const postConverter = {
  toFirestore(post: Post): FirebaseFirestore.DocumentData {
    return {title: post.title, author: post.author};
  },
  fromFirestore(
    snapshot: FirebaseFirestore.QueryDocumentSnapshot
  ): Post {
    const data = snapshot.data();
    return new Post(data.title, data.author);
  }
};

const querySnapshot = await Firestore()
  .collectionGroup('posts')
  .withConverter(postConverter)
  .get();
for (const doc of querySnapshot.docs) {
  const post = doc.data();
  post.title; // string
  post.toString(); // Should be defined
  post.someNonExistentProperty; // TS error
}

withConverter(converter)

withConverter<NewAppModelType, NewDbModelType extends firestore.DocumentData = firestore.DocumentData>(converter: firestore.FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionGroup<NewAppModelType, NewDbModelType>;
Parameter
NameDescription
converter FirebaseFirestore.FirestoreDataConverter<NewAppModelType, NewDbModelType>
Returns
TypeDescription
CollectionGroup<NewAppModelType, NewDbModelType>
Type Parameters
NameDescription
NewAppModelType
NewDbModelType