collectionGroup method

Query<Map<String, Object?>> collectionGroup(
  1. String collectionId
)

Returns a Query over the documents of every collection or subcollection in the database whose id is collectionId.

Every collection whose path ends with collectionId is included, at any depth. Throws an ArgumentError if collectionId is empty or contains a slash. Filters and orderings on a collection group query may need a collection group index on the backend; the backend answers with an error that links to its creation.

// Every landmark of every city.
final QuerySnapshot<Map<String, Object?>> landmarks = await firestore
    .collectionGroup('landmarks')
    .where('type', isEqualTo: 'museum')
    .get();
for (final QueryDocumentSnapshot<Map<String, Object?>> doc in landmarks) {
  print(doc.reference.path);
}

Implementation

Query<Map<String, Object?>> collectionGroup(String collectionId) {
  checkNotNull(collectionId, 'Provided collection ID must not be null.');
  if (collectionId.isEmpty) {
    throw ArgumentError(
      'Function Firestore.collectionGroup() cannot be called with an empty '
      'collectionId.',
    );
  }
  if (collectionId.contains('/')) {
    throw ArgumentError(
      "Invalid collection ID '$collectionId' passed to function "
      "Firestore.collectionGroup(). Collection IDs must not contain '/'.",
    );
  }

  return QueryInternal.create(
    core.Query(ResourcePath.empty, collectionGroup: collectionId),
    this,
  );
}