#StackBounty: #mongodb How to toggle an element in array in mongoDB

Bounty: 50

I have an array of string (ObjectIds) in mongoDB.
I want to toggle an element in that array: If that element exists in the array, remove it, otherwise push that element.

Example:

arr = ["5b608c0769698c3990c64ef3","5b608c0769698c3990c64ef4"]
element = ["5b608c0769698c3990c64ef3"]

Final array:

arr = ["5b608c0769698c3990c64ef4"]

My use-case:

I am creating a blog website and in the Blog Schema I am storing the id of each user who has liked that blog, so that I can highlight the like button when blog is shown to the user.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.