Starting Mongo 4.2
, it's also possible to use a slightly different syntax:
// { name: "book", tags: { words: ["abc", "123"], lat: 33, long: 22 } }
db.collection.update({}, [{ $unset: ["tags.words"] }], { many: true })
// { name: "book", tags: { lat: 33, long: 22 } }
The update method can also accept an aggregation pipeline (note the squared brackets signifying the use of an aggregation pipeline).
This means the $unset
operator being used is the aggregation one (as opposed to the "query" one), whose syntax takes an array of fields.