Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase Firestore嵌套集合架构重构_Firebase_Google Cloud Firestore - Fatal编程技术网

Firebase Firestore嵌套集合架构重构

Firebase Firestore嵌套集合架构重构,firebase,google-cloud-firestore,Firebase,Google Cloud Firestore,我们正在考虑firestore数据库的模式设计,并对使用子集合来组织数据感兴趣。假设我们有数据来模拟船只,他们的航程和他们必须保存的日志。我们可以用嵌套集合对其进行建模,如下所示: // {} signifies a collection {Ships} 001 {Voyages} voy1 {Logs} 12:01 This was all good 12:11 Things are still good voy2 {Logs}

我们正在考虑firestore数据库的模式设计,并对使用子集合来组织数据感兴趣。假设我们有数据来模拟船只,他们的航程和他们必须保存的日志。我们可以用嵌套集合对其进行建模,如下所示:

// {} signifies a collection

{Ships}
  001
  {Voyages}
    voy1
    {Logs}
      12:01 This was all good
      12:11 Things are still good
    voy2
    {Logs}
      09:00 Setting sail
      13:00 Iceberg, dead ahead
  002
  {Voyages}
    voy3
    {Logs}
      16:00 Ship 002 is lost

// get all the voyages for ship 001
db.doc('Ships/001');

// get info and logs for voyage 1
db.doc('Ships/001/Voyage/voy1');

// get all the Logs across all voyages
collectionGroup('Logs');
这很好,因为它允许这样的东西:

// {} signifies a collection

{Ships}
  001
  {Voyages}
    voy1
    {Logs}
      12:01 This was all good
      12:11 Things are still good
    voy2
    {Logs}
      09:00 Setting sail
      13:00 Iceberg, dead ahead
  002
  {Voyages}
    voy3
    {Logs}
      16:00 Ship 002 is lost

// get all the voyages for ship 001
db.doc('Ships/001');

// get info and logs for voyage 1
db.doc('Ships/001/Voyage/voy1');

// get all the Logs across all voyages
collectionGroup('Logs');
还有我们可以用嵌套集合做的所有其他好事情

嵌套集合帮助我们描述每个实体之间的关系,并轻松地挑选相关数据,而无需在客户端进行任何连接


然而,我的问题是,如果我们确定模式中的关系不正确,会发生什么?例如,我们发现日志不一定与航行相关,因为船舶在不航行时甚至会携带日志?或者,船只可以分为舰队——比船只高一级。我们必须迁移模式吗?我们必须通过加入客户端代码和依赖嵌套结构来混合我们的关系吗?

这两种潜在的变化都可以通过不嵌套文档来处理。而是考虑一组像这样的根级集合…

Fleets
  fleet0: {}

Ships
  ship0: { fleet: ref-to-a-fleet } // problem 2. no migration,
                                   // just add a fleet "foreign key"
  ...

Voyages
  voyage0: { ship: ref-to-a-ship }
  ...

Logs
  log0: { voyage: ref-to-a-voyage }
  log1: { ship: ref-to-a-ship }      // problem 1. "foreign key" can be to any
                                     // other collection
这会稍微改变您的查询模式,但要持久。当前设置的一个很好的特性是,集合可以用路径来描述,但是(因为firestore提供了索引),它有点不漂亮,但大致上与这样做一样有效

// original design
db.doc('Ships/001');

// new design
let ship = db.doc('Ships/001');
db.collection('voyages').where('ship', '==', ship);
然后可以向两个引用发送一个
get()
,以获取相同的querySnapshot

编辑:

。我认为这个结论模棱两可。它指出:

  • 正如我所做的那样,顶级(
    其中id==
    )上的限定查询速度大约与嵌套集合中的查询速度一样快
  • 顶级组织允许一个人查询跨多个父级的内容(这对你来说可能非常重要:如果你想让所有航程都到达某个港口,不管船是什么?)
  • 另一方面:在我的建议中,如果您想在任何查询中获得排序结果,您必须创建一个复合索引(很简单,但需要额外的开发步骤)
  • 另外,另一方面,在顶级集合中编写安全规则可能更难,例如当规则依赖于父级中的数据时(尽管在安全规则中也可以使用
    get
    s)

  • 但总的来说,由于主要关注的是未来的灵活性,我认为我已经证明了顶级系列提供了更好的解决方案。

    您希望我用这种结构的另一种想法来回答问题吗?谢谢您的回答。因此,在我看来,目前使用嵌套集合似乎没有太多优势,特别是在灵活性有限的情况下。我想那是对的。我想我在firestore指南的某个地方看到一位医生也同意,但我现在似乎找不到。