In MongoDB, you can remove a specific object from an array within a document. To accomplish this, you'll need to utilize the $pull operator.
The syntax for the $pull operator when removing an entire object from an array is as follows:
{ $pull: { arrayName: { property: value } } }
In the provided example, you're attempting to remove the item with an id of 23 from the items array within the document with the _id of 5150a1199fac0e6910000002. However, your original approach is incorrect as you were attempting to use the pull operator on a field (id) rather than on the desired object.
To correctly remove the entire object from the array, you need to use the following query:
db.mycollection.update( { '_id': ObjectId("5150a1199fac0e6910000002") }, { $pull: { items: { id: 23 } } }, false, // Upsert true, // Multi );
This query will remove the item with an id of 23 from the items array within the specified document.
The above is the detailed content of How do I Remove a Specific Object from an Array in MongoDB?. For more information, please follow other related articles on the PHP Chinese website!