To remove an object from array with JavaScript MongoDB, we call update
with the $pull
operator.
For instance, we write
db.myCollection.update(
{ _id: ObjectId("5150a1199fac0e6910000002") },
{ $pull: { items: { id: 23 } } },
false,
true
);
to call update
to get the element with object ID 5150a1199fac0e6910000002
and remove the item in items
with _id
23 from each entry with $pull
.
The 3rd argument is false
means we disable upsert.
And the 4th argument is true
means we do the operation on all items
entries.