使用分区查询来查询集合组

使用分区查询来查询集合组

代码示例

Go

如需向 Firestore 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证

cities := client.CollectionGroup("cities")

// Get a partioned query for the cities collection group, with a maximum
// partition count of 10
partitionedQueries, err := cities.GetPartitionedQueries(ctx, 10)
if err != nil {
	return fmt.Errorf("GetPartitionedQueries: %w", err)
}

fmt.Printf("Collection Group query partitioned to %d queries\n", len(partitionedQueries))

// Retrieve the first query and iterate over the documents contained.
query := partitionedQueries[0]
iter := query.Documents(ctx)
for {
	doc, err := iter.Next()
	if err == iterator.Done {
		break
	}
	if err != nil {
		return fmt.Errorf("documents iterator: %w", err)
	}
	fmt.Println(doc.Data())
}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器