Node driver - $unset on a property of embedded documents in an array?

My searching hasn’t returned a satisfactory result yet!

If a Widget has many Gizmos, and Gizmos have an optional color, how can I remove all colors from a Widget’s Gizmos?

I thought it would be:

  { _id: widgetId },
  { $unset: {'gizmos.color': 1 }

I can’t find any documentation that specifies how $unset works in the embedded documents array context.

I was reading a S.O. post (which I can’t link because I can’t find it again) that seemed to indicate that there are some options that have to be passed to make the unset work on multiple embedded documents.

I see that there are some update options - but they don’t seem useful.

Please provide sample documents that we can cut-n-paste directly into our system.

If I understand correctly gizmos is an array of objects. Some of the objects have a field named color. You want to remove the field color from all objects.

I am not too sure if there is a way to do it directly. One approach that could work is using the $set with aggregation syntax:

db.widgets.updateOne( query , [ { "$set" : ... } ] )

1 - I would use $map on gizmos to produce a temporary _gizmos by using $objectToArray
2 - Then a $map on _gizmos that uses $filter on each element that remove k:color
3 - A final $map that uses $arrayToObject on _gizmos elements to reconstruct an updated gizmos