Currently, my code just adds any new flagDtos with $addToSet regardless if item.name is unique or not unique, but I want to change this so that:
- if
item.nameis unique , Add the newflagDtosobject - else update the
flagDtoif it matches aitem.name.
How can I make the $addToSet based off a condition?
async addFlagsToFeature(_id: number, flagDtos: FlagDto[]): Promise<FeatureResponse> {
const filter: FilterQuery<FeatureDocument> = { _id };
let success;
var flagIds = [];
return this.featureModel
.findOneAndUpdate({ filter, $addToSet: { flags: flagDtos } })
.then((doc) => {
const featureDto = new FeatureDto();
featureDto.mapFromSchema(doc);
featureDto.flags.forEach((item) => {
flagIds.push(new FlagDto({ featureFlagId: item.featureFlagId }));
success = new SuccessFeatureResponseDto(featureDto.id, flagIds);
});
return new FeatureResponse(success, null);
})
}
Here's my FlagDto class
export class FlagDto {
featureFlagId?: string;
name: string;
}