Through an aggregation pipeline, I get a list of objects, with a conditional checkin list:
/* 1 */
{
"_id" : ObjectId("600ebcb7f1128cd4517f2336"),
"deskId" : "deskA",
"checkin" : []
}
/* 2 */
{
"_id" : ObjectId("600ebcb7f1128cd4517f2338"),
"deskId" : "deskB",
"checkin" : [
{
"_id" : ObjectId("602a7a4483e6fe1a7f5fe909"),
"date" : ISODate("2021-02-15T13:42:28.712Z"),
"phone" : "nico",
}
]
}
Which I want to turn into
/* 1 */
{
"_id" : ObjectId("600ebcb7f1128cd4517f2336"),
"deskId" : "deskA",
"phone" : ""
}
/* 2 */
{
"_id" : ObjectId("600ebcb7f1128cd4517f2338"),
"deskId" : "deskB",
"phone" : "nico"
}
As in:
- empty string when the array is empty
- phone field when checkin.phone is present
- At this stage of the pipeline, the checkin field of type array has zero or one element
I tried $unwind, but because the checkin field can be a list with zero element, top element are being skipped.
preserveNullAndEmptyArraysoption for$unwind.