Saturday 27 May 2017

How to remove array item if exists or push if not?

I would like to add the item if it is not in the array. I would like to remove the item if it is in the array.

collection.update(
    {id: req.user, going: {$nin: [req.body.id]}},
    {
        $addToSet: {going: req.body.id}
    }
);

collection.update(
    {id: req.user, going: {$in: [req.body.id]}},
    {
        $pull: {going: req.body.id},
    }
);

These are correct. But if I run both of them it adds and removes one fast.



via Markus Ivancsics

No comments:

Post a Comment