How to update value of the nth position element inside an array?
I want to update index 2 of category_id.
One solution is ($pop or $pull) and $addToSet.
Is there any other solution?
Array ( [_id] => MongoId Object ( [$id] => 4f93ae990c211da128000004 )
[name] => UnderWater Photography Tips
[category_id] => Array (
[0] => MongoId Object ( [$id] => 4f93a5050c211da328000001 )
[1] => MongoId Object ( [$id] => 4f93a8860c211da128000003 )
[2] => )
[user_id] => MongoId Object ( [$id] => 4f92565a0c211dd706000001 ) )
You can do this by using the array element by position functionality. In PHP, that would look like doing:
On the shell, you would run:
When I execute the following script, it shows exactly what happens:
Now run it: