如何在Firebase Cloud数据库中清空数组?

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

How to clear array in Firebase Cloud database?

问题

我在我的云数据库中有一个令牌数组。我想要清除它(保持一个空的数据库)。我尝试过:

connectedGroup.collection("cars").document(carName).update("tokens", FieldValue.arrayUnion());

它真的会清除数组吗?感觉有点奇怪,因为我以为 arrayUnion 需要一个对象。在数据库中清除数组内容的正确方法是什么?我还考虑过使用:

connectedGroup.collection("cars").document(carName).update("tokens", new ArrayList<>());

哪种方式更好?

英文:

I have a an array of tokens in my Cloud database. I want to clear it (keep an empty database). I tried:

connectedGroup.collection(&quot;cars&quot;).document(carName).update(&quot;tokens&quot;, FieldValue.arrayUnion());

Does it really cleans the array? It feels strange because I thought arrayUnion is expecting an object. What is the proper way to clear array's content in the database? I also thought to use:

connectedGroup.collection(&quot;cars&quot;).document(carName).update(&quot;tokens&quot;, new ArrayList&lt;&gt;());

Which is better?

答案1

得分: 1

FieldValue.arrayUnion()用于在数组字段中添加新元素,如其API文档所述。如果您想确保数组字段为空,只需像第二个示例中一样将空数组写入其中。

英文:

FieldValue.arrayUnion() is used when you want to add new elements to an array field, as described in its API documentation. If you want to make sure an array field is empty, then just write an empty array to it as you are in the second example.

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

发表评论

匿名网友

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

确定