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

Firebase 9.0最新版是否提供创建Firestore嵌套集合的方法

Firebase 9.0 嵌套集合使用说明

Firestore 原生支持嵌套子集合数据结构,没有单独的「创建嵌套集合」专属API:只要在指定文档的子路径下写入任意文档,对应的嵌套子集合会自动生成,无需提前做初始化操作。

  • 若使用 Firebase 9 的 compat 兼容包,你当前写的链式调用代码可以直接正常运行,不需要调整写法:
let snapshot = await firebase.firestore()
.collection('route')
.doc('0bayKbCiAchc0Vy9XuxT')
.collection('qa')
.get()
  • 若使用 Firebase 9 推荐的模块化可摇树SDK,无法沿用旧版链式挂载写法,需要通过路径拼接或基于上级引用构建的方式操作嵌套集合,查询示例如下:
// 先引入模块化SDK对应方法
import { getFirestore, collection, getDocs } from "firebase/firestore";

const db = getFirestore();
// 构建嵌套集合引用,支持两种写法
// 写法1:直接传入全段路径
const qaCollectionRef = collection(db, "route", "0bayKbCiAchc0Vy9XuxT", "qa");
// 写法2:基于上级文档引用构建子集合
// import { doc } from "firebase/firestore";
// const routeDocRef = doc(db, "route", "0bayKbCiAchc0Vy9XuxT");
// const qaCollectionRef = collection(routeDocRef, "qa");

// 执行查询获取快照
const snapshot = await getDocs(qaCollectionRef);

补充说明:Firestore 中不存在空集合,当某集合下所有文档被删除时,集合会自动消失,无需手动删除;向对应子集合路径写入第一条文档时,集合会自动创建,不存在单独的集合创建接口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 00:15:43