【问题标题】:Firestore Golang CollectionGroup To Delete individual SubCollection DocumentFirestore Golang CollectionGroup 删除单个子集合文档
【发布时间】:2020-09-22 05:04:56
【问题描述】:

我能够使用 CollectionGroup 访问所有满足条件的子集合文档(记录)。然后我能够在循环中迭代以删除 subCollection 文档。问题,虽然它工作得很好,但却是一个黑客。有没有更好的方法来使用 Golang 删除 Firestore 中的子集合?

    it := clientdb.CollectionGroup("mychildSubcollection").Where(...mycondition).OrderBy("myfield", firestore.Desc).Documents(context.Background())
for {
    doc, err := it.Next()
    if err == iterator.Done {
        break
    }
    if err != nil {
        // return err
    }
    
        // Strucure of the doc.Ref is --> &{0xc0000d6788 projects/myproj/databases/(default)/documents/myparentCollection/Ki8sr65sKIoZaCviCp/mychildSubcollection/JwvKbuyRTGx5wZaCviCp myparentCollection/Ki8sr65sKIoZaCviCp/mychildSubcollection/JwvKbuyRTGx5wZaCviCp JwvKbuyRTGx5wZaCviCp}        

    // fmt.Println(doc.Ref.ID)
    // fmt.Println(doc.Ref.Path)
    // fmt.Println(doc.Ref.Parent.Path)
    // fmt.Println(doc.Ref.Parent.ID)

    path1 := doc.Ref.Parent.Path
    path2 := path1[0: strings.LastIndex(path1, "myparentCollection")]
    path3 := strings.Replace(path1, path2, "", -1)
    clientdb.Collection(path3).Doc(doc.Ref.ID).Delete(context.Background())    // Regular collection command, to delete the subcollection
}

至少为了减少黑客攻击,这也可能有所帮助 -> 如您所见,doc.Ref 提供了三个字段,显示子集合文档的完整路径 [doc.Ref.Path 或 doc.Ref.Parent.Path] id(doc.Ref.ID),如何访问结构中的中间字段:“myparentCollection/Ki8sr65sKIoZaCviCp/mychildSubcollection/JwvKbuyRTGx5wZaCviCp”

谢谢!

【问题讨论】:

  • "虽然它工作得很好,但它是一个 hack" - 它不是一个 hack。这正是你应该做的。没有简单的命令可以删除集合或集合组中的所有内容。
  • 一如既往地感谢道格!我认为这很笨拙。 path1 := doc.Ref.Parent.Path path2 := path1[0: strings.LastIndex(path1, "myparentCollection")] path3 := strings.Replace(path1, path2, "", -1)
  • 既然是这样做的方式,你能把它放在答案中并接受它,以便其他社区成员可以从中受益吗?
  • 我刚才做了。谢谢。 fyi-看起来我无法接受自己的解决方案。
  • 谢谢!啊,好吧,如果是答案的形式,就已经可以了。

标签: go google-cloud-firestore


【解决方案1】:

以上被接受为解决方案。我要添加的唯一评论是,如果有更好的方法来提取集合的路径,它将更加通用。如您所见,我使用实际的集合名称 [myparentCollection] 来提取路径。效果很好,但通用的会更好。

now := time.Now() // start deleting old ones, even ones that still have one second left to expire.
it := clientdb.CollectionGroup("mychildSubcollection").Where("expireafter", "<", now.Add(-1*time.Second)).OrderBy("myfield", firestore.Desc).Documents(context.Background())
    for {
        doc, err := it.Next()
        if err == iterator.Done {
            break
        }
        if err != nil {
            // return err
        }
        
            // Strucure of the doc.Ref is --> &{0xc0000d6788 projects/myproj/databases/(default)/documents/myparentCollection/Ki8sr65sKIoZaCviCp/mychildSubcollection/JwvKbuyRTGx5wZaCviCp myparentCollection/Ki8sr65sKIoZaCviCp/mychildSubcollection/JwvKbuyRTGx5wZaCviCp JwvKbuyRTGx5wZaCviCp}        
    
        // fmt.Println(doc.Ref.ID)
        // fmt.Println(doc.Ref.Path)
        // fmt.Println(doc.Ref.Parent.Path)
        // fmt.Println(doc.Ref.Parent.ID)
    
         // using some logic with strings below to get (extract) the collection path example: myparentCollection/Ki8sr65sKIoZaCviCp/mychildSubcollection, so the delete can use it and delete the particular document under the sub collection.
        path1 := doc.Ref.Parent.Path
        path2 := path1[0: strings.LastIndex(path1, "myparentCollection")]
        path3 := strings.Replace(path1, path2, "", -1)
        clientdb.Collection(path3).Doc(doc.Ref.ID).Delete(context.Background())    // Regular collection command, to delete the subcollection
    }

【讨论】:

    猜你喜欢
    • 2020-02-17
    • 2018-11-01
    • 1970-01-01
    • 2020-01-04
    • 2018-03-22
    • 2020-09-08
    • 2018-07-20
    相关资源
    最近更新 更多