i have some objects with a field which, for space reasons, uses three possible encodings:
- field is missing (0 values)
{
}
- field is a scalar (1 value)
{
the_field: ObjectIdentifier(...)
}
- field is an array (>=2 values)
{
the_field: [ObjectIdentifier(...), ObjectIdentifier(...), ... ]
}
i have documents that contain arrays of this object:
{
_id: ...
r: [
{ },
{ the_field: ObjectIdentifier(...) },
{ the_field: [ObjectIdentifier(...), ObjectIdentifier(...), ... ] },
...
]
}
now i need to perform a $lookup
using the ObjectIdentifier
s stored in each element of $r
, but i am struggling to come up with an aggregation expression that can flatten these objects into an array of ObjectIdentifier
s. what is the best way to accomplish this?
for context, case #2 does not currently exist in our database, only case #1 and #3 exist, so we are currently using:
{ $ifNull: [ $$self.the_field, [] ] }
inside a $reduce
.
but i had a hard time generalizing this to cover case #2. since it does not currently exist, we could use a different name for the field if that would help.