Avoid adding duplicate record to my index when running 'index.addObjects'

Hello everyone,

I’m having an issue currently with index.addObjects(). Whenever I run it, it adds duplicate data to the index instead of leaving out the already present data.

Is there a function I can use to stop adding duplicate records?

Hi @adetonaabiodun12.

Do you set the objectID in your objects? It’s always best to set it yourself, then the addObjects method will be able to update objects with the same objectID.

1 Like

Hey @julienbordeau,

I didn’t set it my self but from what am seeing in my index, it is added automatically by algolia.

Do I need to set it myself?

Yeah, otherwise Algolia will consider any object as new. An ObjectID is generated automatically to let you push any data but it’s best to set it yourself. You will need this objectID to delete, update or partially update a record via the API so it’s best if you know it.

Typically this will be your primary key in your database.

My bad. I didn’t know this.

How do I do this? I’m using mongoose.