You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust Firestore库FirestoreListCollectionIdsParams字段含义咨询

Rust Firestore库list_collection_ids参数解析及问题修正

FirestoreListCollectionIdsParams字段说明

  • parent:指定查询的父资源路径,格式必须符合Firestore的资源路径规则。如果要查询根级集合,路径应为projects/{你的项目ID}/databases/(default)/documents;如果要查询某个文档下的子集合,路径是projects/{你的项目ID}/databases/(default)/documents/{文档路径}。不能直接传入集合名(如"Cards"),这会导致路径不合法,进而触发认证或查询错误。
  • page_size:单次查询返回的最大集合ID数量,取值范围在1-1000之间(受Firestore服务限制),用于分页控制结果数量。
  • page_token:分页查询的续传令牌,仅在第一次查询返回next_page_token时,后续查询才需要传入该值以获取下一页结果。初始查询时应设为None,不可随意传入集合名等无效值。

你的代码问题及修正

当前代码存在两处关键错误,会导致查询失败(包括认证类错误):

  1. parent路径格式错误:直接传入"Cards"不是合法的Firestore资源路径,需替换为符合规则的完整路径。
  2. page_token误用:初始查询无需传入page_token,应设为None。

修正后的示例代码:

let db = FirestoreDb::with_options_service_account_key_file(
    FirestoreDbOptions::new("你的项目ID".to_string()),
    "key.json".into(),
)
.await
.unwrap();

// 查询所有根级集合的参数配置
let param = FirestoreListCollectionIdsParams {
    parent: Some("projects/你的项目ID/databases/(default)/documents".to_owned()),
    page_size: 10,
    page_token: None,
};

let result = db.list_collection_ids(param).await?;

println!("查询到的集合ID: {:?}", result.collection_ids);

// 处理分页场景:如果存在下一页令牌,继续查询
if let Some(next_token) = result.next_page_token {
    let next_param = FirestoreListCollectionIdsParams {
        parent: Some("projects/你的项目ID/databases/(default)/documents".to_owned()),
        page_size: 10,
        page_token: Some(next_token),
    };
    let next_result = db.list_collection_ids(next_param).await?;
    println!("下一页集合ID: {:?}", next_result.collection_ids);
}

内容的提问来源于stack exchange,提问作者kaxa gvimraze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 01:27:23