I'm looking for the proper way to remove all elements from an array field (across all documents) in Mongodb - these appear to be equivalent, which is recommended: (or perhaps some other way?)
db.collection.update({}, { $pull : { 'myArray': {} }}, {multi:true} )
or
db.collection.update({}, { $set : {'myArray': [] }} , {multi:true} )
To delete all documents that match a deletion criteria, pass a filter parameter to the deleteMany() method. The method returns a document with the status of the operation. For more information and examples, see deleteMany() .
In MongoDB, the $unset operator is used to delete a particular field. The value specified in the $unset expression does not make any impact on the operation. The $unset has no effect when the field does not exist in the document. name of the column or field to be deleted.
The $set
variant will be faster as the $pull
will have to do calculations on arrays. I am actually not even certain whether it will work, as you're not really removing any elements with your query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With