如何在以列表形式存储在 firestore 字段中时为键设置唯一值

How to have unique value for a key while storing in firestore field as a list

我在“反应”字段下存储哈希图列表,使用下面的代码

 val emoji = HashMap<String, Long>()
        emoji[_uid] = Type
        repository.getPostReference(postId).update("reacts", FieldValue.arrayUnion(emoji))

它正在像下面这样存储,有什么方法可以确保它们只有一个键值?

Is there any way i can ensure their is only one value for a key?

不适用于 arrayUnion():如 doc 中所述,“arrayUnion() 将元素添加到数组 但仅添加不存在的元素 ".

由于您将 HashMap 存储在数组中,因此这两个元素不被视为等效。 (键相似但值不同)。

您需要在前端读取数组,更新它并将其写回数据库。