Hello, so I basically want to only remove one item from an array in my inventory. I read that $pull would technically do the job but it will remove all other same value items in the array. Is there any other way I can approach on how to remove one item only?
Unfortunately the operator to change the first match only ā$ā does not let you remove an array element, if you use it with $unset you can make it null but not remove in one operation.
What you want to do can be done but needs you to use the expressive update syntax and $set the new value of inventory to new value computed using $cond and $reduce. You just stepped over the line from basic to expert mode with this one Iām afraid.
If you look at the twofewercount you get the idea - you express the new value compared to the existing value using aggregation expressions so a simple $subtract makes sense.
with $reduce you say start with a $$value (what it will resolve to) equal to "initialValueā Iterate over the array āinputā and for each value āinā the array make $$value equal to an expression.
The āinā expression here says if $$this (current array item) == āborgorā and $$value.stilllooking is true (the value we set at the start) then - set stilllooking to false (we found it) and donāt add $$this it to the array in $$value . otherwise add the current element to the array and keep $$value.stilllooking as it is.