说明
返回数据库中不同集合和嵌套级别中的所有文档。
语法
Node.js
const results = await db.pipeline()
.database()
.execute();
客户端示例
Web
// Count all documents in the database const results = await execute(db.pipeline() .database() .aggregate(countAll().as("total")) );
Swift
// Count all documents in the database let results = try await db.pipeline() .database() .aggregate([CountAll().as("total")]) .execute()
Kotlin
// Count all documents in the database val results = db.pipeline() .database() .aggregate(AggregateFunction.countAll().alias("total")) .execute()
Java
// Count all documents in the database Task<Pipeline.Snapshot> results = db.pipeline() .database() .aggregate(AggregateFunction.countAll().alias("total")) .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Count # Count all documents in the database results = client.pipeline().database().aggregate(Count().as_("total")).execute()
Java
// Count all documents in the database Pipeline.Snapshot results = firestore.pipeline().database().aggregate(countAll().as("total")).executppets.java
行为
如需使用 database 阶段,它必须作为流水线中的第一个阶段出现。
从 database 阶段返回的文档顺序不稳定,不应依赖。可使用后续的排序阶段来获得确定性的顺序。
例如,对于以下文档:
Node.js
await db.collection('cities').doc('SF').set({name: 'San Francsico', state: 'California', population: 800000});
await db.collection('states').doc('CA').set({name: 'California', population: 39000000});
await db.collection('countries').doc('USA').set({name: 'United States of America', population: 340000000});
database 阶段可用于检索数据库中的所有文档。
Node.js
const results = await db.pipeline()
.database()
.sort(field('population').ascending())
.execute();
此查询会生成以下文档:
{name: 'San Francsico', state: 'California', population: 800000}
{name: 'California', population: 39000000}
{name: 'United States of America', population: 340000000}