简体   繁体   English

Cloud Firestore Swift:如何删除文档查询

[英]Cloud Firestore Swift: How to delete a query of documents

I would like to delete all documents in my collection Usernames that has the field UID as the current user's ID. 我想删除我的集合用户名中所有具有UID字段作为当前用户ID的文档。 This is my code so far: 到目前为止,这是我的代码:

let uid = Auth.auth().currentUser!.uid
db.collection("Usernames").whereField("UID", isEqualTo: uid).delete

but here comes the error: 但是这里出现错误:

Value of type 'Query' has no member 'delete'. 类型“查询”的值没有成员“删除”。

Any special technique to this? 有什么特殊的技巧吗? Thanks! 谢谢!

The guide shows you how to delete data. 该指南向您展示如何删除数据。 It also notes that deleting entire collections shouldn't be done from the client. 它还指出,不应从客户端删除整个集合。 There's no way to delete from a query -- you will have to get all the documents and delete them individually. 无法从查询中删除-您将必须获取所有文档并分别删除它们。 If there is the potential for lots of documents, then you should do this server-side. 如果存在大量文档的可能性,则应在服务器端进行操作。 If you know for sure there will only be a few, you can query them, get the doc references, and then delete them, like this: 如果您确定只有少数几个,可以查询它们,获取文档引用,然后删除它们,如下所示:

let uid = Auth.auth().currentUser!.uid
db.collection("Usernames").whereField("UID", isEqualTo: uid).getDocuments() { (querySnapshot, err) in
  if let err = err {
    print("Error getting documents: \(err)")
  } else {
    for document in querySnapshot!.documents {
      document.reference.delete()
    }
  }
}

If this is something that will need to be done frequently, then this is a good opportunity to examine your data structure. 如果需要经常执行此操作,那么这是检查数据结构的好机会。 Maybe make a collection where user's Usernames are listed under their uid . 也许进行一个收集,其中用户的用户名在其uid之下列出。 Then you could just query that single doc and use it to reference the other ones to delete. 然后,您可以查询单个文档,并使用它引用要删除的其他文档。

Usernames: {
   byUID: {
        uid1: {
            funky_monkey: true,
            goodTimes: true
        }
   }
   byUsername: {
        funky_monkey: {
            UID: uid1
        }
   }
}

This is just one suggestion. 这只是一个建议。 There are definitely other options that may be better for your app. 肯定还有其他选项可能对您的应用程序更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算 swift 中 firestore 查询的文档数? - How can I count the documents from a firestore query in swift? 从Cloud Firestore获取四个随机但唯一的文档 - Swift - Getting Four Random, but Unique, Documents from a Cloud Firestore - Swift 在 Swift 中存储异步 Cloud Firestore 查询结果 - Storing asynchronous Cloud Firestore query results in Swift 如何在 Swift 中创建 n 小时后删除 firebase Cloud Firestore 中的 Collections? - How to delete the Collections in firebase Cloud Firestore after n hours of its creation in Swift? 如何检索 Cloud Firestore 文档并在 TableView 中显示数据? - How to retrieve Cloud Firestore documents and display data in a TableView? 如果文档存在于 swift 中,如何从 Firestore 中删除它? - How to delete a document from firestore if it exists in swift? SwiftUI:如何遍历 Cloud Firestore 集合中的文档并获取数据? - SwiftUI: How to iterate over Documents in Cloud Firestore Collection and get Data? 在 Cloud Firestore (Swift) 中运行 collectionGroup 查询时崩溃 - Crash when running collectionGroup query in Cloud Firestore (Swift) Firestore删除子集合中的所有文档 - Firestore delete all documents in a subcollection 如何使用Swift在iOS的Firebase Firestore中的文档中获取arrayList - How to get the arrayList inside documents in firebase firestore in ios using swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM