[英]How delete documents from a Firestore collection with a 'where' clause in Web v9
I'm struggling to complete the following code:我正在努力完成以下代码:
const myCollection = collection(db, 'myCollection');
const mycollectionQuery = query(myCollection, where("field1", "==", "xyz"));
myCollectionSnapshot.forEach((doc) => {
deleteDoc(?);
});
Advice on what to supply inside the deleteDoc
call would be much appreciated.关于在
deleteDoc
调用中提供什么的建议将不胜感激。 I was pretty confident it would be doc.id
, but this returns a "Cannot use 'in' operator to search for '_delegate' in undefined" error message我非常有信心它会是
doc.id
,但这会返回“无法使用 'in' 运算符来搜索未定义中的 '_delegate'”错误消息
In V9, the deleteDoc()
method takes a DocumentReference, as documented here .在 V9 中,
deleteDoc()
方法采用 DocumentReference,如此处所述。
As you can see from the documentation of your forEach()
, the doc
variable will be a QueryDocumentSnapshot, that is a specialization of DocumentSnapshot.正如您从
forEach()
的文档中看到的, doc
变量将是一个 QueryDocumentSnapshot,它是 DocumentSnapshot 的特化。 It means that, if you want to retrieve the DocumentReference for your doc
, you just need to reference to it via doc.ref
.这意味着,如果您想为您的
doc
检索 DocumentReference,您只需要通过doc.ref
引用它。
In other words, to delete the document you'll need to do this:换句话说,要删除文档,您需要执行以下操作:
myCollectionSnapshot.forEach((doc) => {
deleteDoc(doc.ref);
});
This is how you delete a document from cloud firestore:这是从 Cloud Firestore 中删除文档的方法:
deleteDoc(doc(db, "reference"))
So, the complete code is:所以,完整的代码是:
const myCollection = collection(db, 'myCollection');
const mycollectionQuery = query(myCollection, where("field1", "==", "xyz"));
myCollectionSnapshot.forEach((doc) => {
deleteDoc(doc(db, doc.ref));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.