I have an array of objects. Each object saves the Activity Types. I need to update one of the objects of the array. I used the _id for updating or deleting the object. So, when I pass id to update the data, I actually find the index of that object in the array and replace the data on that index with the new data.
Here the issue is that my _id also changes because I am not using $set or not directly updating using query. I am changing the data using map function and saving it to a mongoDB cursor which later at some place gets updated.
Below is the code and the output of the code.
organization.activityTypes = organization.activityTypes || [];
let findInd = organization.activityTypes.findIndex(x => x._id.toString() == (activityType.id?activityType.id.toString() : ''));
if(activityType.isDelete == true ) {
if(findInd > -1) organization.activityTypes.splice(findInd,1);
}
else if(findInd > -1) {
organization.activityTypes[findInd] ={
_id : activityType._id,
teams : activityType.teams == [] ? allTeams : activityType.teams ,
name: toTitleCase(activityType.name),
isDelete: activityType.isDelete,
custom : activityType.custom
}
}
else {
organization.activityTypes.push({
teams: activityType.teams == [] ? allTeams : activityType.teams,
name: toTitleCase(activityType.name),
isDelete: activityType.isDelete,
custom: activityType.custom
})
}
})
The output of the above code is given below. This is the image of the output.
So, I changed the name of the activity Type. Initally its id is 5a15438482900b2aa6dc7ab1 then it converts to 5a15498cb024a02b4628e33f. How can I prevent this change of _id in the database on every update?