How to remove object from array with MongoDB?

Sometimes, we want to remove object from array with MongoDB.

In this article, we’ll look at how to remove object from array with MongoDB.

How to remove object from array with MongoDB?

To remove object from array with MongoDB, we can use the $pull operator.

For instance, we write

db.mycollection.update({
    '_id': ObjectId("5150a1199fac0e6910000002")
  }, {
    $pull: {
      items: {
        id: 23
      }
    }
  },
  false,
  true,
);

to call update with an object that has the $pull property with items.id set to 23 to remove the items entry with id 23.

We pass in false to disable upsert and pass in true to let us update multiple entries.

Conclusion

To remove object from array with MongoDB, we can use the $pull operator.