1

Is it possible to remove elements from multiple subarrays inside of one big array? My structure looks something like this:

{
  "_id": {
    "$oid": ""
  },
  "users": [
    {
      "friends": [
        "751573404103999569"
      ]
    },
    {
      "friends": [
        "220799458408005633"
      ]
    }
  ]
}

I have a friend id and I need to remove it from all the "friends" arrays in the "users" array

1 Answer 1

1

You can do with the positional operator $[] as follow:

db.no_more_friends.update({ "users.friends":"the_friend_id"  },{ $pull:{"users.$[].friends":"the_friend_id"}} ,{multi:true})

Just take in consideration that with {multi:true} it will perform the user id removal to all friends sub-arrays in all documents where "the_friend_id" is found

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.