Question: I have an object and within that object, I have an array of objects. I'm trying to remove a single object from within the array. My code right now removes the entire object and I'm not sure why. Any thoughts?

eg: `Collection example`

```
"profiles": {
  "names": [
    {
      "first": "Joe",
      "last": "First",
      "uniqueId": "075ae7fda11"
    },
    {
      "first": "Sam",
      "last": "Last",
      "uniqueId": "1488096533"
    }
  ]
}

```

eg. `Update method`

```
MyCollection.update({_id: userId}, {
  $unset: {
    profiles: {
      'names.$.first': 'Joe'
    }
  }
});

```

Answer: 
Use [$pull](https://docs.mongodb.com/manual/reference/operator/update/pull/#pull) operator to remove all matching objects.

```
MyCollection.update({_id: userId}, {
  "$pull": {
    "profiles.names": {
      "first": "Joe"
    }
  }
});

```

To remove single object using [$unset](https://docs.mongodb.com/manual/reference/operator/update/unset/).When you use positional [$](https://docs.mongodb.com/manual/reference/operator/update/positional/#update) operator with update operation the array field must appear as part of the query document [check docs](https://docs.mongodb.com/manual/reference/operator/update/positional/#up._S_)

```
MyCollection.update({_id: userId,"profiles.names.first":"Joe"},{ 
   "$unset":{
      "profiles.names.$":""
   }  
});

```

**Important Note** : `$unset` replaces the matching element with null rather than removing the matching element from the array. This behavior keeps consistent the array size and element positions.

So the execution of above query will update your names array and it will look like:

```
"profiles": {
  "names": [
    null,
    {
      "first": "Sam",
      "last": "Last",
      "uniqueId": "1488096533"
    }
  ]
}

```

Removing of object completely is not yet supported by monngodb, see the open bug:
<https://jira.mongodb.org/browse/SERVER-1014>
This is how you can do it,

```
MyCollection.update({ _id: userId) },
    {
        "$pull":
            {
                "profiles.names":
                    {
                        "first": "Joe"
                    }
            }
    });

```