Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding/updating/removing nested array in algolia

Tags:

algolia

In the algolia documentation, they specify that you can manipulate arrays like this:

// adding
index.partialUpdateObject({
  myfield: {
    value: 'myvalue',
    _operation: 'Add'
  },
  objectID: 'myID'
})

/removing
index.partialUpdateObject({
  myfield: {
    value: 'myvalue',
    _operation: 'Remove'
  }
})

This works well when the array is a string or number. However imagine that I have this document structure, where arrays are actually nested objects:

{
  first_name: String,
  last_name: String,
  subjects: [
    {
      itemId: String,
      title: String,
      randomField: String,
      dateAdded: Date
    }
  ]
}

In this case the algolia documentation is very unclear. For example, imagine the following scenarios:

  • I want to update the randomField field of a particular array item. I want to be able to update a nested array item by itemId.
  • I want to be able to add or remove nested array items. In this case, what do I pass into the "value" field when doing a partialUpdateObject.

Is this kind of thing possible in Algolia? What would be your recommendations?

like image 950
Zach Avatar asked Apr 07 '26 09:04

Zach


1 Answers

It's not possible to add/update/remove a specific attribute of a nested array using the partilUpdateObject function.

You get it right by fetching the object, modifying and updating it after. :)

like image 158
aseure Avatar answered Apr 10 '26 03:04

aseure