I am looking for a more efficient way to add or remove an ID from an array (target
) in an Articles
object, based on whether it already exists. Currently, I am using the following approach:
var isExisting = Articles.findOne({ _id }).target.indexOf(mID) > -1
if (isExisting === false) {
Articles.update(
{ _id },
{ $addToSet: { target: mID } }
)
} else if (isExisting === true) {
Articles.update(
{ _id },
{ $pull: { target: mID } }
)
}
Could there be a more optimal way to achieve this without the need for if/else statements and a minimum of two database operations?