1

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?

Amanpreet
  • 219
  • 3
  • 10

1 Answers1

1

I found that if I change the entire object at a particular index position, the _id of that indexed object will also get changed but if I only change fields of that objects,the object data will get updated but not the _id. My updated code is written below.

args.activityTypes.map((activityType)=> {
                    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].teams = activityType.teams == [] ? allTeams: activityType.teams;
                        organization.activityTypes[findInd].name = toTitleCase(activityType.name);
                        organization.activityTypes[findInd].isDelete = Object.prototype.hasOwnProperty.call(activityType,'isDelete') ? activityType.isDelete :organization.activityTypes[findInd].isDelete;
                        organization.activityTypes[findInd].custom = Object.prototype.hasOwnProperty.call(activityType,'custom') ? activityType.custom :organization.activityTypes[findInd].custom;
                    }
                    else {
                        organization.activityTypes.push({
                            teams: activityType.teams == [] ? allTeams : activityType.teams,
                            name: toTitleCase(activityType.name),
                            isDelete: activityType.isDelete,
                            custom: activityType.custom
                        })
                    }
                })
Amanpreet
  • 219
  • 3
  • 10