如何使用 WriteBatch 在列表中移除文档引用?

huangapple go评论72阅读模式
英文:

How to remove a Document Reference in list with WriteBatch?

问题

我正在使用Firebase Cloud数据库。这个问题与之前的问题类似,但现在我对DocumentReferences很感兴趣。我查询了一个集合并获得了一系列文档。每个文档都包含一个字段cars,其中包含DocumentReferences。每个DocumentReference引用一个表示汽车的文档。鉴于carsPath,它是指向汽车文档的路径,我想要从该列表中删除DocumentReferences。我正在使用WriteBatch。如果cars只包含路径,可以通过以下方式轻松实现:

batch.update(snapshot.getReference(), "cars", FieldValue.arrayRemove(carPath));

如果我们有DocumentReferences而不是Strings,是否有类似的方法呢?

英文:

I'm using Firebase Cloud database. This question is similar to the previous one but now I'm interested in DocumentReferences. I queried a collection and got a list of documents. Each document contains a field cars which contains DocumentReferences. Each DocumentReference references a document that represents a car. Given the carsPath which is a path to the car document, I want to remove the DocumentReferences in that list. I'm using WriteBatch. If cars would contain just the paths, it would be easy by using:

batch.update(snapshot.getReference(), "cars", FieldValue.arrayRemove(carPath));

Is there a way to do something similar if we have DocumentReferences instead of Strings?

答案1

得分: 1

只需将 DocumentReference 对象传递给 FieldValue.arrayRemove(),而不是传递字符串。引用的路径必须与数组中的内容完全匹配。

英文:

Just pass DocumentReference object instead of a string to FieldValue.arrayRemove(). The path of the reference must match exactly what's in the array.

huangapple
  • 本文由 发表于 2020年9月15日 06:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63892722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定