With a schema like this :
{
"_id" : ObjectId("513fe2b85b51eafc15000023"),
"tags" : [
"House",
"Red"
]
}
How would you do the following:
If the tag exists remove it, if it doesn't exist add it.
The operator $addToSet and $pull. $addToSet will only add the tag if it doesn't exist. $pull will remove all instances of the tag.
As @assylias mentioned, you would construct a full update command with the _id presumabley, or in conjunction with {tags: tagInQuestion} to only operate on a document that has tagInQuestion etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With