Skip to content

FR: FieldValue arrayUnion/arrayRemove for array with objects #1918

Open
@emadalam

Description

@emadalam

[REQUIRED] Describe your environment

  • Operating System version: macOS 10.14.3
  • Browser version: Google Chrome 74.0.3729.169
  • Firebase SDK version: 6.0.2
  • Firebase Product: database

[REQUIRED] Describe the problem

Steps to reproduce:

firebase.firestore.FieldValue.arrayUnion and firebase.firestore.FieldValue.arrayRemove are great if the array is made up of primitive values. However to update a field value which is made up of array of objects, it's impossible to perform the operation without querying the actual data and performing the union/without operation on the client.

While this is alright if we already have queried the data, but in cases where we haven't queried the data to begin with, it's an unnecessary overhead to make this request. What's worse is when we haven't subscribed to the document updates and are trying to manipulate the array with the stale values, it's going to cause more harm than good, defeating the purpose of allowing array operations with arrayUnion and arrayRemove.

Relevant Code:

// existing document data
const data = {
  uid: 'xxxx'
  key: 'value',
  uploadedDocuments: [{
    uid: 'yyyy',
    name: 'something.png',
    url: '....'
  }, {
    uid: 'zzzz',
    name: 'something-else.png',
    url: '....'
  }]
}

// update document but it will add new object to the list
firebase.firestore
  .doc('myCollection/xxxx')
  .update({
    uploadedDocuments:
      firebase.firestore.FieldValue.arrayUnion({
        uid: 'yyyy',
        name: 'something.png',
        url: '....'
      })
  })

What is desirable is an option to set uniqueBy parameter or something similar.

firebase.firestore.doc('myCollection/xxxx')
  .update({
    uploadedDocuments:
      firebase.firestore.FieldValue.arrayUnion({
        uid: 'yyyy',
        name: 'something.png',
        url: '....'
      }, firebase.firestore.FieldValue.uniqueBy('uid'))
  })

// signature
firebase.firestore.FieldValue.arrayUnion(
  ...values,
  firebase.firestore.FieldValue.uniqueBy(key)
)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions